mbien commented on code in PR #7239: URL: https://github.com/apache/netbeans/pull/7239#discussion_r1558690317
########## platform/api.dashboard/src/org/netbeans/modules/dashboard/WidgetComponents.java: ########## @@ -0,0 +1,403 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.dashboard; + +import java.awt.Color; +import java.awt.Component; +import java.awt.Cursor; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Graphics; +import java.awt.Image; +import java.awt.Insets; +import java.awt.event.ActionEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.net.URI; +import javax.swing.AbstractAction; +import javax.swing.Action; +import javax.swing.BorderFactory; +import javax.swing.Icon; +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JLabel; +import javax.swing.JSeparator; +import javax.swing.JTextArea; +import javax.swing.UIManager; +import javax.swing.border.Border; +import org.netbeans.spi.dashboard.WidgetElement; +import org.netbeans.spi.dashboard.WidgetElement.ActionElement; +import org.netbeans.spi.dashboard.WidgetElement.ComponentElement; +import org.netbeans.spi.dashboard.WidgetElement.ImageElement; +import org.netbeans.spi.dashboard.WidgetElement.LinkElement; +import org.netbeans.spi.dashboard.WidgetElement.SeparatorElement; +import org.netbeans.spi.dashboard.WidgetElement.TextElement; +import org.openide.awt.HtmlBrowser; +import org.openide.awt.StatusDisplayer; +import org.openide.util.Exceptions; +import org.openide.util.ImageUtilities; +import org.openide.util.NbBundle; + +/** + * Default creation of JComponents for WidgetElements. + */ [email protected]({ + "# {0} - link URI", + "TXT_statusLink=Open {0}" +}) +final class WidgetComponents { + + private WidgetComponents() { + + } + + static JComponent titleComponentFor(String title) { + MultiLineText text = new MultiLineText(title); + Font font = text.getFont(); + text.setFont(font.deriveFont(font.getSize() * 1.4f)); + return text; + } + + static JComponent componentFor(WidgetElement element) { + if (element instanceof TextElement textElement) { + return componentForText(textElement); + } else if (element instanceof ImageElement imageElement) { + return componentForImage(imageElement); + } else if (element instanceof ActionElement actionElement) { + return componentForAction(actionElement); + } else if (element instanceof LinkElement linkElement) { + return componentForLink(linkElement); + } else if (element instanceof SeparatorElement sepElement) { + return componentForSeparator(sepElement); + } else if (element instanceof ComponentElement cmpElement) { + return cmpElement.component(); + } + return null; + } + + private static JComponent componentForText(TextElement element) { + MultiLineText text = new MultiLineText(element.text()); + TextElement.Kind kind = element.kind(); + if (kind == TextElement.Kind.SUBHEADING) { + Font font = text.getFont(); + text.setFont(font.deriveFont(font.getSize() * 1.1f)); + } else if (kind == TextElement.Kind.ASIDE || kind == TextElement.Kind.UNAVAILABLE) { + Font font = text.getFont(); + text.setFont(font.deriveFont(font.getSize() * 0.9f)); + Color color = UIManager.getColor("controlDkShadow"); + if (color != null) { + text.setForeground(color); + } + } + return text; + } + + private static JComponent componentForImage(ImageElement element) { + Image image = ImageUtilities.loadImage(element.resourcePath(), true); + Icon icon = ImageUtilities.image2Icon(image); + JLabel label = new JLabel(icon); + label.setMinimumSize(new Dimension(0, 0)); + return label; + } + + private static JComponent componentForAction(ActionElement element) { + if (element.asLink()) { + return new LinkButton(element.action(), element.showIcon()); + } else { + return new WidgetButton(element.action(), element.showIcon()); + } + } + + private static JComponent componentForLink(LinkElement element) { + Action action = new URIOpenAction(element.text(), element.link()); + if (element.asButton()) { + return new WidgetButton(action, false); + } else { + return new LinkButton(action, false); + } + } + + private static JComponent componentForSeparator(WidgetElement.SeparatorElement element) { + JSeparator sep = new JSeparator(); + sep.setPreferredSize(new Dimension(75, 8)); + return sep; + } + + private static class MultiLineText extends JTextArea { + + private MultiLineText(String text) { + super(text); + setEditable(false); + setCursor(null); + setOpaque(false); + // some LAFs (looking at you Nimbus!) require transparent bg color + setBackground(new Color(0, 0, 0, 0)); + setFocusable(false); + setBorder(BorderFactory.createEmptyBorder()); + setMargin(new Insets(0, 0, 0, 0)); + Font labelFont = UIManager.getFont("Label.font"); + if (labelFont != null) { + setFont(labelFont); + } + Color labelColor = UIManager.getColor("Label.foreground"); + if (labelColor != null) { + setForeground(labelColor); + } + setWrapStyleWord(true); + setLineWrap(true); + } + + } + + private static abstract class AbstractWidgetButton extends JButton { + + private final boolean allowIcon; + + private AbstractWidgetButton(Action action, boolean allowIcon) { + super(action); + this.allowIcon = allowIcon; + if (!allowIcon) { + setIcon(null); + } + setHorizontalAlignment(LEFT); + setToolTipText(null); + addMouseListener(new MouseAdapter() { + + @Override + public void mouseEntered(MouseEvent e) { + onMouseEnter(); + } + + @Override + public void mouseExited(MouseEvent e) { + onMouseExit(); + } + + }); + } + + void onMouseEnter() { + Action action = getAction(); + if (action != null) { + Object status = action.getValue(Action.SHORT_DESCRIPTION); + if (status instanceof String statusText) { + StatusDisplayer.getDefault().setStatusText(statusText); + } + } + } + + void onMouseExit() { + StatusDisplayer.getDefault().setStatusText(""); + } + + @Override + protected void actionPropertyChanged(Action action, String propertyName) { + if (allowedPropertyChange(propertyName)) { + super.actionPropertyChanged(action, propertyName); + } + } + + private boolean allowedPropertyChange(String propertyName) { + return switch (propertyName) { + case Action.SMALL_ICON -> + allowIcon; + case Action.LARGE_ICON_KEY -> + allowIcon; + case Action.SHORT_DESCRIPTION -> + false; + case Action.LONG_DESCRIPTION -> + false; + default -> + true; + }; Review Comment: probably going to have to fix the formatter for this one day so that witch statements can be written like: ```java return switch (propertyName) { case Action.SMALL_ICON -> allowIcon; case Action.LARGE_ICON_KEY -> allowIcon; case Action.SHORT_DESCRIPTION -> false; case Action.LONG_DESCRIPTION -> false; default -> true; }; ``` ########## platform/api.dashboard/src/org/netbeans/modules/dashboard/DashboardTopComponent.java: ########## @@ -0,0 +1,222 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.dashboard; + +import java.util.List; +import org.netbeans.api.dashboard.DashboardManager; +import org.netbeans.api.settings.ConvertAsProperties; +import org.netbeans.spi.dashboard.DashboardDisplayer; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.windows.TopComponent; +import org.openide.util.NbBundle.Messages; +import org.openide.windows.OnShowing; +import org.openide.windows.WindowManager; +import org.openide.windows.WindowSystemEvent; +import org.openide.windows.WindowSystemListener; + +/** + * Default Dashboard Displayer top component. + */ +@ConvertAsProperties( + dtd = "-//org.netbeans.modules.dashboard//Dashboard//EN", + autostore = false +) [email protected]( + preferredID = "DashboardDisplayer", + persistenceType = TopComponent.PERSISTENCE_ONLY_OPENED +) [email protected](mode = "editor", openAtStartup = false, position = 0) +@Messages({ + "CTL_DashboardTopComponent=Dashboard", + "HINT_DashboardTopComponent=The Dashboard window" +}) +public final class DashboardTopComponent extends TopComponent { + + private static final String ID = "DashboardDisplayer"; + + private List<DashboardDisplayer.WidgetReference> widgetRefs; + private DashboardPanel dashboardPanel; + + public DashboardTopComponent() { + initComponents(); + scrollPane.getVerticalScrollBar().setUnitIncrement(20); + setName(Bundle.CTL_DashboardTopComponent()); + setToolTipText(Bundle.HINT_DashboardTopComponent()); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents + private void initComponents() { + + scrollPane = new javax.swing.JScrollPane(); + dashContainer = new javax.swing.JPanel(); + emptyLabel = new javax.swing.JLabel(); + + setLayout(new java.awt.BorderLayout()); + + scrollPane.setBorder(null); + scrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); + scrollPane.setViewportBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1)); + + dashContainer.setLayout(new java.awt.GridBagLayout()); + + org.openide.awt.Mnemonics.setLocalizedText(emptyLabel, org.openide.util.NbBundle.getMessage(DashboardTopComponent.class, "DashboardTopComponent.emptyLabel.text")); // NOI18N + dashContainer.add(emptyLabel, new java.awt.GridBagConstraints()); + + scrollPane.setViewportView(dashContainer); + + add(scrollPane, java.awt.BorderLayout.CENTER); + }// </editor-fold>//GEN-END:initComponents + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JPanel dashContainer; + private javax.swing.JLabel emptyLabel; + private javax.swing.JScrollPane scrollPane; + // End of variables declaration//GEN-END:variables + @Override + public void componentShowing() { + if (dashboardPanel != null) { + dashboardPanel.notifyShowing(); + } + } + + @Override + public void componentHidden() { + if (dashboardPanel != null) { + dashboardPanel.notifyHidden(); + } + } + + void writeProperties(java.util.Properties p) { + // better to version settings since initial version as advocated at + // http://wiki.apidesign.org/wiki/PropertyFiles + p.setProperty("version", "1.0"); //NOI18N + // TODO store your settings + } + + void readProperties(java.util.Properties p) { + String version = p.getProperty("version"); + // TODO read your settings according to their version + } + + private void installWidgets(DefaultDashboardDisplayer displayer, + List<DashboardDisplayer.WidgetReference> widgetRefs) { + if (this.widgetRefs != null) { + if (this.widgetRefs.equals(widgetRefs)) { + return; + } + } + this.widgetRefs = List.copyOf(widgetRefs); + // TODO allow panel to be reconfigured at runtime, and detach removed widgets + this.dashboardPanel = new DashboardPanel(displayer, widgetRefs); + dashContainer.removeAll(); + dashContainer.add(this.dashboardPanel); + dashboardPanel.notifyShowing(); + } + + static void show(DefaultDashboardDisplayer displayer, + List<DashboardDisplayer.WidgetReference> widgetRefs) { + DashboardTopComponent dashTC = find(); + if (dashTC == null) { + return; + } + if (widgetRefs.isEmpty()) { + dashTC.close(); + return; + } + dashTC.installWidgets(displayer, widgetRefs); + if (!dashTC.isOpened()) { + dashTC.openAtTabPosition(0); + } + dashTC.requestActive(); + } + + private static DashboardTopComponent find() { + TopComponent tc = WindowManager.getDefault().findTopComponent(ID); + if (tc instanceof DashboardTopComponent dashTC) { + return dashTC; + } else { + return null; + } Review Comment: this made me curious if the new intanceof/cast syntax supports the ternary operator, turns out it does! ```java return tc instanceof DashboardTopComponent dashTC ? dashTC : null; ``` ########## platform/api.dashboard/src/org/netbeans/modules/dashboard/DashboardTopComponent.java: ########## @@ -0,0 +1,222 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.dashboard; + +import java.util.List; +import org.netbeans.api.dashboard.DashboardManager; +import org.netbeans.api.settings.ConvertAsProperties; +import org.netbeans.spi.dashboard.DashboardDisplayer; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.windows.TopComponent; +import org.openide.util.NbBundle.Messages; +import org.openide.windows.OnShowing; +import org.openide.windows.WindowManager; +import org.openide.windows.WindowSystemEvent; +import org.openide.windows.WindowSystemListener; + +/** + * Default Dashboard Displayer top component. + */ +@ConvertAsProperties( + dtd = "-//org.netbeans.modules.dashboard//Dashboard//EN", + autostore = false +) [email protected]( + preferredID = "DashboardDisplayer", + persistenceType = TopComponent.PERSISTENCE_ONLY_OPENED +) [email protected](mode = "editor", openAtStartup = false, position = 0) +@Messages({ + "CTL_DashboardTopComponent=Dashboard", + "HINT_DashboardTopComponent=The Dashboard window" +}) +public final class DashboardTopComponent extends TopComponent { + + private static final String ID = "DashboardDisplayer"; + + private List<DashboardDisplayer.WidgetReference> widgetRefs; + private DashboardPanel dashboardPanel; + + public DashboardTopComponent() { + initComponents(); + scrollPane.getVerticalScrollBar().setUnitIncrement(20); + setName(Bundle.CTL_DashboardTopComponent()); + setToolTipText(Bundle.HINT_DashboardTopComponent()); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents + private void initComponents() { + + scrollPane = new javax.swing.JScrollPane(); + dashContainer = new javax.swing.JPanel(); + emptyLabel = new javax.swing.JLabel(); + + setLayout(new java.awt.BorderLayout()); + + scrollPane.setBorder(null); + scrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); + scrollPane.setViewportBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1)); + + dashContainer.setLayout(new java.awt.GridBagLayout()); + + org.openide.awt.Mnemonics.setLocalizedText(emptyLabel, org.openide.util.NbBundle.getMessage(DashboardTopComponent.class, "DashboardTopComponent.emptyLabel.text")); // NOI18N + dashContainer.add(emptyLabel, new java.awt.GridBagConstraints()); + + scrollPane.setViewportView(dashContainer); + + add(scrollPane, java.awt.BorderLayout.CENTER); + }// </editor-fold>//GEN-END:initComponents + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JPanel dashContainer; + private javax.swing.JLabel emptyLabel; + private javax.swing.JScrollPane scrollPane; + // End of variables declaration//GEN-END:variables + @Override + public void componentShowing() { + if (dashboardPanel != null) { + dashboardPanel.notifyShowing(); + } + } + + @Override + public void componentHidden() { + if (dashboardPanel != null) { + dashboardPanel.notifyHidden(); + } + } + + void writeProperties(java.util.Properties p) { + // better to version settings since initial version as advocated at + // http://wiki.apidesign.org/wiki/PropertyFiles + p.setProperty("version", "1.0"); //NOI18N + // TODO store your settings + } + + void readProperties(java.util.Properties p) { + String version = p.getProperty("version"); + // TODO read your settings according to their version + } Review Comment: this looks like it won't be needed and can be removed? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
