This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository jaxx. See https://gitlab.nuiton.org/nuiton/jaxx.git commit 0d082061890fa74f595b9a1d060042ee18d0f465 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Sep 13 12:29:38 2016 +0200 Introduce a AboutPanelBuilder (See #4024) --- .../org/nuiton/jaxx/widgets/about/AboutPanel.jaxx | 52 ++---- .../jaxx/widgets/about/AboutPanelBuilder.java | 187 +++++++++++++++++++++ .../jaxx/widgets/about/AboutPanelHandler.java | 151 +++-------------- .../i18n/jaxx-widgets-about_en_GB.properties | 3 +- .../i18n/jaxx-widgets-about_es_ES.properties | 3 +- .../i18n/jaxx-widgets-about_fr_FR.properties | 3 +- .../src/main/resources/icons/action-close.png | Bin 0 -> 518 bytes 7 files changed, 231 insertions(+), 168 deletions(-) diff --git a/jaxx-widgets-about/src/main/java/org/nuiton/jaxx/widgets/about/AboutPanel.jaxx b/jaxx-widgets-about/src/main/java/org/nuiton/jaxx/widgets/about/AboutPanel.jaxx index 84f1f06..596c526 100644 --- a/jaxx-widgets-about/src/main/java/org/nuiton/jaxx/widgets/about/AboutPanel.jaxx +++ b/jaxx-widgets-about/src/main/java/org/nuiton/jaxx/widgets/about/AboutPanel.jaxx @@ -20,53 +20,26 @@ #L% --> -<Table insets='1' background='{getBackgroundColor()}' border='{BorderFactory.createLineBorder(Color.BLACK, 1)}'> +<Table insets='1' border='{BorderFactory.createLineBorder(Color.BLACK, 1)}'> <import> java.awt.Color - java.awt.Frame javax.swing.BorderFactory javax.swing.SwingConstants - </import> - <AboutPanelHandler id='handler' constructorParams='this'/> + static org.nuiton.i18n.I18n.t; + </import> <String id='title' javaBean='null'/> <String id='iconPath' javaBean='null'/> - <String id='aboutText' javaBean='null'/> - <String id='bottomText' javaBean='null'/> - <Color id='backgroundColor' javaBean='null'/> - <script><![CDATA[ -public void setLicenseFile(String filename) { - handler.setLicenseFile(filename); -} - -public void setThirdpartyFile(String filename) { - handler.setThirdpartyFile(filename); -} -public void addTab(String tabTitle, String resourceName) { - handler.addTab(tabTitle, resourceName); -} -public void buildTopPanel() { - handler.buildTopPanel(); -} - -public void init() { - handler.init(); -} - -public void showInDialog(Frame ui, boolean undecorated) { - handler.showInDialog(ui, undecorated); -} - -void $afterCompleteSetup() { - handler.$afterCompleteSetup(); +public void display() { + handler.display(this); } ]]> @@ -76,6 +49,10 @@ void $afterCompleteSetup() { <JPanel background='{Color.WHITE}' layout='{new BorderLayout()}'> <JLabel text='{SwingUtil.getStringValue(getTitle())}' visible='{getTitle() != null}' font-size='12' constraints='BorderLayout.CENTER'/> + + <JToolBar constraints='BorderLayout.EAST' opaque="false" borderPainted="false" floatable="false"> + <JButton id='close' borderPainted="false" focusable="false" rolloverEnabled="false"/> + </JToolBar> </JPanel> </cell> </row> @@ -86,15 +63,7 @@ void $afterCompleteSetup() { </row> <row> <cell weighty='1' fill='both'> - <JTabbedPane id='tabs'> - <tab title="aboutframe.about"> - <JScrollPane id='aboutContent' border='{null}'> - <JEditorPane contentType='text/html' editable='false' border='{null}' - text='{SwingUtil.getStringValue(getAboutText())}' - onHyperlinkUpdate='SwingUtil.openLink(event)'/> - </JScrollPane> - </tab> - </JTabbedPane> + <JTabbedPane id='tabs'/> </cell> </row> <row> @@ -102,7 +71,6 @@ void $afterCompleteSetup() { <JPanel layout='{new BorderLayout()}'> <JLabel id='bottomLabel' constraints='BorderLayout.CENTER' horizontalAlignment='{SwingConstants.CENTER}' text='{SwingUtil.getStringValue(getBottomText())}' visible='{getBottomText() != null}'/> - <JButton id='close' action='{handler.closeAction}' constraints='BorderLayout.EAST'/> </JPanel> </cell> </row> diff --git a/jaxx-widgets-about/src/main/java/org/nuiton/jaxx/widgets/about/AboutPanelBuilder.java b/jaxx-widgets-about/src/main/java/org/nuiton/jaxx/widgets/about/AboutPanelBuilder.java new file mode 100644 index 0000000..314d418 --- /dev/null +++ b/jaxx-widgets-about/src/main/java/org/nuiton/jaxx/widgets/about/AboutPanelBuilder.java @@ -0,0 +1,187 @@ +package org.nuiton.jaxx.widgets.about; + +import com.google.common.base.Preconditions; +import com.google.common.io.Resources; +import jaxx.runtime.SwingUtil; +import org.nuiton.util.Resource; + +import javax.swing.AbstractAction; +import javax.swing.Action; +import javax.swing.Icon; +import javax.swing.JComponent; +import javax.swing.JDialog; +import javax.swing.JEditorPane; +import javax.swing.JLabel; +import javax.swing.JScrollPane; +import javax.swing.SwingUtilities; +import javax.swing.event.HyperlinkEvent; +import javax.swing.event.HyperlinkListener; +import java.awt.Frame; +import java.awt.event.ActionEvent; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +import static org.nuiton.i18n.I18n.t; +import static org.nuiton.jaxx.widgets.about.AboutPanelHandler.CLOSE_ACTION; + +/** + * Created on 12/09/16. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class AboutPanelBuilder { + + protected final AboutPanel aboutPanel; + + protected final Frame parent; + + public AboutPanelBuilder(Frame parent) { + Preconditions.checkNotNull(parent); + this.parent = parent; + this.aboutPanel = new AboutPanel(); + this.aboutPanel.putClientProperty(AboutPanelHandler.PARENT, parent); + + Action closeAction = new AbstractAction(CLOSE_ACTION) { + private static final long serialVersionUID = 1L; + + @Override + public void actionPerformed(ActionEvent e) { + JDialog container = aboutPanel.getParentContainer(JDialog.class); + container.dispose(); + } + + }; + closeAction.putValue(Action.NAME, null); + closeAction.putValue(Action.SMALL_ICON, SwingUtil.getUIManagerActionIcon(CLOSE_ACTION)); + closeAction.putValue(Action.SHORT_DESCRIPTION, t("aboutframe.close")); + aboutPanel.getClose().setAction(closeAction); + } + + public static AboutPanelBuilder builder(Frame parent) { + return new AboutPanelBuilder(parent); + } + + public AboutPanelBuilder setTitle(String title) { + aboutPanel.setTitle(title); + return this; + } + + public AboutPanelBuilder setIconPath(String iconPath) { + aboutPanel.setIconPath(iconPath); + if (iconPath != null) { + Icon logoIcon = Resource.getIcon(iconPath); + aboutPanel.getTopPanel().add(new JLabel(logoIcon)); + } + return this; + } + + public AboutPanelBuilder setBottomText(String bottomText) { + aboutPanel.setBottomText(bottomText); + return this; + } + + public AboutPanelBuilder undecorated() { + aboutPanel.putClientProperty(AboutPanelHandler.UNDECORATED, true); + return this; + } + + public AboutPanelBuilder addAboutTab(String aboutText, boolean html) { + return addTab0(t("aboutframe.about"), aboutText, html); + } + + public AboutPanelBuilder addDefaultLicenseTab(String projectName, boolean html) { + return addLicenseTab("META-INF/" + projectName + "-LICENSE.txt", html); + } + + public AboutPanelBuilder addLicenseTab(String resourceName, boolean html) { + String tabContent = load(resourceName); + addTab0(t("aboutframe.license"), tabContent, html); + return this; + } + + public AboutPanelBuilder addDefaultThirdPartyTab(String projectName, boolean html) { + return addThirdPartyTab("META-INF/" + projectName + "-THIRD-PARTY.txt", html); + } + + public AboutPanelBuilder addThirdPartyTab(String resourceName, boolean html) { + String tabContent = load(resourceName); + return addTab0(t("aboutframe.thirdparty"), tabContent, html); + } + + public AboutPanelBuilder addDefaultChangelogTab(String projectName, boolean html) { + return addChangelogTab("META-INF/" + projectName + "-CHANGELOG.txt", html); + } + + public AboutPanelBuilder addChangelogTab(String resourceName, boolean html) { + String tabContent = load(resourceName); + return addTab0(t("aboutframe.changelog"), tabContent, html); + } + + public AboutPanelBuilder addTab(String tabTitle, String tabContent, boolean html) { + return addTab0(tabTitle, tabContent, html); + } + + public AboutPanelBuilder addTabFromResource(String tabTitle, String resourceName, boolean html) { + String tabContent = load(resourceName); + return addTab0(tabTitle, tabContent, html); + } + + public AboutPanelBuilder addTab(String tabTitle, JComponent component) { + JScrollPane scrollPane = new JScrollPane(); + scrollPane.setBorder(null); + aboutPanel.tabs.addTab(tabTitle, component); + return this; + } + + public AboutPanel build() { + return aboutPanel; + } + + protected AboutPanelBuilder addTab0(String tabTitle, String tabContent, boolean html) { + + JScrollPane scrollPane = new JScrollPane(); + scrollPane.setBorder(null); + aboutPanel.tabs.addTab(tabTitle, scrollPane); + + final JEditorPane jEditorPane = new JEditorPane(); + jEditorPane.setBorder(null); + if (html) { + // must be done before set in text + jEditorPane.setContentType("text/html"); + } + jEditorPane.setText(tabContent); + jEditorPane.setEditable(false); + jEditorPane.setFont(jEditorPane.getFont().deriveFont((float) 11)); + jEditorPane.addHyperlinkListener(new HyperlinkListener() { + @Override + public void hyperlinkUpdate(HyperlinkEvent e) { + SwingUtil.openLink(e); + } + }); + + scrollPane.getViewport().add(jEditorPane); + SwingUtilities.invokeLater(new Runnable() { + + @Override + public void run() { + jEditorPane.setCaretPosition(0); + } + }); + + return this; + } + + protected String load(String resourceName) { + String result = null; + try { + result = Resources.asCharSource(Resources.getResource(resourceName), StandardCharsets.UTF_8).read(); + + } catch (IOException ex) { + // ignore it + } + if (result == null) { + result = "resource " + resourceName + " not found"; + } + return result; + } +} diff --git a/jaxx-widgets-about/src/main/java/org/nuiton/jaxx/widgets/about/AboutPanelHandler.java b/jaxx-widgets-about/src/main/java/org/nuiton/jaxx/widgets/about/AboutPanelHandler.java index 7737af9..7c3cca5 100644 --- a/jaxx-widgets-about/src/main/java/org/nuiton/jaxx/widgets/about/AboutPanelHandler.java +++ b/jaxx-widgets-about/src/main/java/org/nuiton/jaxx/widgets/about/AboutPanelHandler.java @@ -23,31 +23,17 @@ package org.nuiton.jaxx.widgets.about; import jaxx.runtime.JAXXUtil; import jaxx.runtime.SwingUtil; -import org.apache.commons.io.IOUtils; +import jaxx.runtime.spi.UIHandler; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.nuiton.util.Resource; -import javax.swing.AbstractAction; -import javax.swing.Action; -import javax.swing.Icon; import javax.swing.JComponent; import javax.swing.JDialog; -import javax.swing.JLabel; import javax.swing.JRootPane; -import javax.swing.JScrollPane; -import javax.swing.JTextArea; import javax.swing.KeyStroke; -import javax.swing.SwingUtilities; -import java.awt.Component; import java.awt.Frame; -import java.awt.event.ActionEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; -import java.io.IOException; -import java.io.InputStream; - -import static org.nuiton.i18n.I18n.t; /** * Handler of the ui {@link AboutPanel}. @@ -55,132 +41,51 @@ import static org.nuiton.i18n.I18n.t; * @author Tony Chemit - chemit@codelutin.com * @since 2.4 */ -public class AboutPanelHandler { +public class AboutPanelHandler implements UIHandler<AboutPanel> { private static final Log log = LogFactory.getLog(AboutPanelHandler.class); - protected final AboutPanel ui; - - public AboutPanelHandler(AboutPanel ui) { - this.ui = ui; - } - - final protected Action closeAction = new AbstractAction("close") { - private static final long serialVersionUID = 1L; - - @Override - public void actionPerformed(ActionEvent e) { - JDialog container = ui.getParentContainer(JDialog.class); - if (container != null) { - container.dispose(); - } else { - ui.setVisible(false); - } - } - }; + public static final String CLOSE_ACTION = "close"; + public static final String UNDECORATED = "undecorated"; + public static final String PARENT = "parent"; - public void setLicenseFile(String filename) { - addTab(t("aboutframe.license"), filename); + @Override + public void beforeInit(AboutPanel aboutPanel) { } - public void setThirdpartyFile(String filename) { - addTab(t("aboutframe.thirdparty"), filename); + @Override + public void afterInit(final AboutPanel aboutPanel) { } - public void addTab(String tabTitle, String resourceName) { - String tabContent = load(resourceName); + public void display(final AboutPanel ui) { - JScrollPane scrollPane = new JScrollPane(); - scrollPane.setBorder(null); - ui.tabs.add(scrollPane); - ui.tabs.setTitleAt(ui.tabs.getTabCount() - 1, tabTitle); - - final JTextArea textArea = new JTextArea(); - textArea.setText(tabContent); - textArea.setEditable(false); - scrollPane.getViewport().add(textArea); - SwingUtilities.invokeLater(new Runnable() { - - @Override - public void run() { - textArea.setCaretPosition(0); - } - }); - - } + boolean undecorated = (Boolean) ui.getClientProperty(UNDECORATED); + Frame parent = (Frame) ui.getClientProperty(PARENT); - public void buildTopPanel() { - // image - JLabel labelIcon; - if (ui.iconPath != null) { - Icon logoIcon = Resource.getIcon(ui.iconPath); - labelIcon = new JLabel(logoIcon); - } else { - labelIcon = new JLabel(); + JDialog dialog = new JDialog(parent, true); + dialog.add(ui); + if (ui.getIconPath() != null) { + dialog.setIconImage(SwingUtil.createIcon(ui.getIconPath()).getImage()); } - ui.topPanel.add(labelIcon); - } - - public void init() { - if (ui.getAboutText() == null) { - ui.tabs.remove(ui.aboutContent); - } - } - - public void showInDialog(Frame ui, boolean undecorated) { - JDialog f = new JDialog(ui, true); - f.add(this.ui); - if (this.ui.iconPath != null) { - f.setIconImage(SwingUtil.createIcon(this.ui.iconPath).getImage()); - } - f.setResizable(true); - f.setSize(550, 450); - f.setUndecorated(undecorated); - JRootPane rootPane = f.getRootPane(); - rootPane.setDefaultButton(this.ui.close); - rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "close"); - rootPane.getActionMap().put("close", closeAction); - f.addWindowListener(new WindowAdapter() { + dialog.setResizable(true); + dialog.setSize(550, 450); + dialog.setUndecorated(undecorated); + dialog.addWindowListener(new WindowAdapter() { @Override public void windowClosed(WindowEvent e) { - Component ui = (Component) e.getSource(); - if (log.isInfoEnabled()) { - log.info("destroy ui " + ui); + if (log.isDebugEnabled()) { + log.debug("destroy ui " + ui); } JAXXUtil.destroy(ui); - JAXXUtil.destroy(AboutPanelHandler.this.ui); } }); - SwingUtil.center(ui, f); - f.setVisible(true); - } + JRootPane rootPane = dialog.getRootPane(); + rootPane.setDefaultButton(ui.getClose()); + rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), CLOSE_ACTION); + rootPane.getActionMap().put(CLOSE_ACTION, ui.getClose().getAction()); - protected String load(String filename) { - InputStream licenseStream = getClass().getResourceAsStream("/" + filename); - String result = null; - try { - if (licenseStream != null) { - result = IOUtils.toString(licenseStream); - } - } catch (IOException ex) { - // ignore it - } finally { - if (licenseStream != null) { - try { - licenseStream.close(); - } catch (IOException ex) { - log.error("could not close file " + filename); - } - } - } - if (result == null) { - result = "resource " + filename + " not found"; - } - return result; - } + SwingUtil.center(parent, dialog); + dialog.setVisible(true); - void $afterCompleteSetup() { - buildTopPanel(); - ui.close.setText(t("aboutframe.ok")); } } diff --git a/jaxx-widgets-about/src/main/resources/i18n/jaxx-widgets-about_en_GB.properties b/jaxx-widgets-about/src/main/resources/i18n/jaxx-widgets-about_en_GB.properties index 180c699..bbbe5c5 100644 --- a/jaxx-widgets-about/src/main/resources/i18n/jaxx-widgets-about_en_GB.properties +++ b/jaxx-widgets-about/src/main/resources/i18n/jaxx-widgets-about_en_GB.properties @@ -1,4 +1,5 @@ aboutframe.about=About +aboutframe.changelog=Changelog +aboutframe.close=Quit (ESCAPE) aboutframe.license=License -aboutframe.ok=OK aboutframe.thirdparty=Third party diff --git a/jaxx-widgets-about/src/main/resources/i18n/jaxx-widgets-about_es_ES.properties b/jaxx-widgets-about/src/main/resources/i18n/jaxx-widgets-about_es_ES.properties index 6f31c24..2623ddc 100644 --- a/jaxx-widgets-about/src/main/resources/i18n/jaxx-widgets-about_es_ES.properties +++ b/jaxx-widgets-about/src/main/resources/i18n/jaxx-widgets-about_es_ES.properties @@ -1,4 +1,5 @@ aboutframe.about=A cerca de +aboutframe.changelog=Histórico +aboutframe.close=Dejar (ESCAPE) aboutframe.license=Licecia -aboutframe.ok=OK aboutframe.thirdparty=Tierce partie diff --git a/jaxx-widgets-about/src/main/resources/i18n/jaxx-widgets-about_fr_FR.properties b/jaxx-widgets-about/src/main/resources/i18n/jaxx-widgets-about_fr_FR.properties index 6653653..9966aa1 100644 --- a/jaxx-widgets-about/src/main/resources/i18n/jaxx-widgets-about_fr_FR.properties +++ b/jaxx-widgets-about/src/main/resources/i18n/jaxx-widgets-about_fr_FR.properties @@ -1,4 +1,5 @@ aboutframe.about=À propos +aboutframe.changelog=Historique +aboutframe.close=Quitter (ESCAPE) aboutframe.license=Licence -aboutframe.ok=OK aboutframe.thirdparty=Tierce partie diff --git a/jaxx-widgets-about/src/main/resources/icons/action-close.png b/jaxx-widgets-about/src/main/resources/icons/action-close.png new file mode 100644 index 0000000..93edaf0 Binary files /dev/null and b/jaxx-widgets-about/src/main/resources/icons/action-close.png differ -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.