diff options
author | Marko Živković <[email protected]> | 2014-06-12 23:50:29 +0000 |
---|---|---|
committer | Marko Živković <[email protected]> | 2014-06-12 23:50:29 +0000 |
commit | e8d19c46aefb7378be44c704d93d3e408b443d2e (patch) | |
tree | 096b24e6a53f23f212c69fcf77e52ca235b11e6c /logo | |
parent | a576088a6e9567fbffd13ce23a32a460a785832d (diff) |
Refactored WelcomeScreen and WorkspaceSettings to extend and reuse functionality of X4SFrame
git-svn-id: https://svn.code.sf.net/p/xlogo4schools/svn/trunk@5 3b0d7934-f7ef-4143-9606-b51f2e2281fd
Diffstat (limited to 'logo')
-rw-r--r-- | logo/src/xlogo/Logo.java | 7 | ||||
-rw-r--r-- | logo/src/xlogo/gui/components/X4SFrame.java | 48 | ||||
-rw-r--r-- | logo/src/xlogo/gui/welcome/WelcomeScreen.java | 346 | ||||
-rw-r--r-- | logo/src/xlogo/gui/welcome/WorkspaceSettings.java | 33 |
4 files changed, 220 insertions, 214 deletions
diff --git a/logo/src/xlogo/Logo.java b/logo/src/xlogo/Logo.java index 86406af..4e495df 100644 --- a/logo/src/xlogo/Logo.java +++ b/logo/src/xlogo/Logo.java @@ -129,12 +129,13 @@ public class Logo { public void actionPerformed(ActionEvent e) { enterApplication(); } - }); + }); + welcomeScreen.showFrame(); } }); //UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel()); } catch(Exception e){ - System.out.println("here " + e.toString()); //TODO error m + e.printStackTrace(); //TODO error m } } @@ -145,7 +146,7 @@ public class Logo { public void enterApplication(){ WorkspaceConfig wc = WSManager.getInstance().getWorkspaceConfigInstance(); - welcomeScreen.dispose(); + welcomeScreen.closeFrame(); generateLanguage(wc.getLanguage()); // Initialize frame diff --git a/logo/src/xlogo/gui/components/X4SFrame.java b/logo/src/xlogo/gui/components/X4SFrame.java index 92296bb..1d06c8c 100644 --- a/logo/src/xlogo/gui/components/X4SFrame.java +++ b/logo/src/xlogo/gui/components/X4SFrame.java @@ -27,8 +27,16 @@ package xlogo.gui.components;
+import java.awt.event.WindowEvent;
+import java.awt.event.WindowStateListener;
+
import javax.swing.JFrame;
+import xlogo.messages.async.AsyncMediumAdapter;
+import xlogo.messages.async.AsyncMessage;
+import xlogo.messages.async.AsyncMessenger;
+import xlogo.messages.async.dialog.DialogMessenger;
+
public abstract class X4SFrame extends X4SGui{
public X4SFrame() {
@@ -36,5 +44,45 @@ public abstract class X4SFrame extends X4SGui{ }
public abstract JFrame getFrame();
+
+ public void showFrame()
+ {
+ setMessageManagerParent();
+ getFrame().setVisible(true);
+ }
+
+ public void closeFrame()
+ {
+ getFrame().dispose();
+ }
+
+ /**
+ * Make this frame the parent for popups and dialogs
+ */
+ protected void setMessageManagerParent()
+ {
+ DialogMessenger.getInstance().setMedium(new AsyncMediumAdapter<AsyncMessage<JFrame>, JFrame>(){
+ public boolean isReady()
+ {
+ return getFrame().isDisplayable();
+ }
+ public JFrame getMedium()
+ {
+ return getFrame();
+ }
+ public void addMediumReadyListener(final AsyncMessenger messenger)
+ {
+ getFrame().addWindowStateListener(new WindowStateListener(){
+
+ @Override
+ public void windowStateChanged(WindowEvent e)
+ {
+ if (getFrame().isDisplayable())
+ messenger.onMediumReady();
+ }
+ });
+ }
+ });
+ }
}
diff --git a/logo/src/xlogo/gui/welcome/WelcomeScreen.java b/logo/src/xlogo/gui/welcome/WelcomeScreen.java index 011081e..baf9b85 100644 --- a/logo/src/xlogo/gui/welcome/WelcomeScreen.java +++ b/logo/src/xlogo/gui/welcome/WelcomeScreen.java @@ -36,10 +36,8 @@ import javax.swing.text.JTextComponent; import java.awt.*; import java.io.IOException; +import xlogo.gui.components.X4SFrame; import xlogo.messages.MessageKeys; -import xlogo.messages.async.AsyncMediumAdapter; -import xlogo.messages.async.AsyncMessage; -import xlogo.messages.async.AsyncMessenger; import xlogo.messages.async.dialog.DialogMessenger; import xlogo.storage.Storable; import xlogo.storage.WSManager; @@ -48,7 +46,6 @@ import xlogo.storage.workspace.WorkspaceConfig; import xlogo.utils.Utils; import xlogo.utils.WebPage; import xlogo.Application; -import xlogo.Logo; /** * This was initially called {@code Selection_Langue} and it was only displayed when the Application was opened for the very first time. @@ -57,24 +54,25 @@ import xlogo.Logo; * <li> Storage Location (master password required) </li> * @author Marko */ -public class WelcomeScreen extends JFrame { - private static final long serialVersionUID = 1L; +public class WelcomeScreen extends X4SFrame { + JFrame frame; + private JLabel label; - private JLabel workspace = new JLabel("Workspace"); - private JLabel username = new JLabel("User"); + private JLabel workspace; + private JLabel username; - private JComboBox workspaceSelection = new JComboBox(); - private JComboBox userSelection = new JComboBox(); + private JComboBox workspaceSelection; + private JComboBox userSelection; - private JButton openWorkspaceSettingsBtn = new JButton("Settings"); - private JButton enterButton = new JButton("Enter"); + private JButton openWorkspaceSettingsBtn; + private JButton enterButton; - private JButton infoButton = new JButton(); - private JButton gplButton = new JButton(); + private JButton infoButton; + private JButton gplButton; - private JPanel panel = new JPanel(); + private JPanel panel; private GroupLayout groupLayout; private ActionListener onApplicationEnterListener; @@ -87,20 +85,150 @@ public class WelcomeScreen extends JFrame { */ public WelcomeScreen(ActionListener onApplicationEnterListener){ this.onApplicationEnterListener = onApplicationEnterListener; + } + + @Override + public JFrame getFrame() + { + return frame; + } + + @Override + protected void initComponent() + { + frame = new JFrame(){ + + private static final long serialVersionUID = -6730403281163492211L; + + @Override + public void dispose() + { + try { + WSManager.getInstance().getGlobalConfigInstance().store(); + } catch (IOException e) { + DialogMessenger.getInstance().dispatchMessage( + translate("ws.error.title"), + translate("storage.could.not.store.gc")); + } + + System.gc(); + super.dispose(); + } + }; // Window - super.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - setIconImage(Toolkit.getDefaultToolkit().createImage(Utils.class.getResource("Icon_x4s.png"))); - setTitle("XLogo4Schools"); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + frame.setIconImage(Toolkit.getDefaultToolkit().createImage(Utils.class.getResource("Icon_x4s.png"))); + frame.setTitle("XLogo4Schools"); + + + workspace = new JLabel("Workspace"); + username = new JLabel("User"); + + workspaceSelection = new JComboBox(); + userSelection = new JComboBox(); + + openWorkspaceSettingsBtn = new JButton("Settings"); + enterButton = new JButton("Enter"); + + infoButton = new JButton(); + gplButton = new JButton(); + + panel = new JPanel(); // The XLogo4Schools logo //ImageIcon logo = Utils.dimensionne_image("Logo_xlogo4schools.png", this); - infoButton.setIcon(createImageIcon("info_icon.png", "Info", 40, 40)); gplButton.setIcon(createImageIcon("gnu_gpl.png", "GPL", 40, 40)); label = new JLabel(createImageIcon("Logo_xlogo4schools.png", "XLogo4Schools", 250, 40)); - // Select workspace combo box initWorkspaceListModel(); + populateUserList(); + + workspaceSettings = new WorkspaceSettings(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + // When the window is closed, repopulate workspace and user lists, update language, enable this window again. + setMessageManagerParent(); + setText(); + populateWorkspaceList(); + populateUserList(); + frame.setEnabled(true); + } + }); + } + + @Override + protected void layoutComponent() + { + frame.getContentPane().add(panel); + + frame.setResizable(false); + infoButton.setBorder(null); + gplButton.setBorder(null); + + infoButton.setOpaque(false); + gplButton.setOpaque(false); + + panel.add(workspace); + panel.add(username); + panel.add(workspaceSelection); + panel.add(userSelection); + panel.add(openWorkspaceSettingsBtn); + panel.add(enterButton); + panel.add(infoButton); + panel.add(gplButton); + + workspaceSelection.setMinimumSize(new Dimension(200, 25)); + userSelection.setMinimumSize(new Dimension(200, 25)); + workspaceSelection.setMaximumSize(new Dimension(200, 25)); + userSelection.setMaximumSize(new Dimension(200, 25)); + + groupLayout = new GroupLayout(panel); + panel.setLayout(groupLayout); + + groupLayout.setAutoCreateGaps(true); + groupLayout.setAutoCreateContainerGaps(true); + + groupLayout.setVerticalGroup( + groupLayout.createSequentialGroup() + .addGroup(groupLayout.createParallelGroup() + .addComponent(gplButton) + .addComponent(infoButton) + .addComponent(label)) + .addGroup(groupLayout.createParallelGroup() + .addComponent(workspace) + .addComponent(workspaceSelection) + .addComponent(openWorkspaceSettingsBtn)) + .addGroup(groupLayout.createParallelGroup() + .addComponent(username) + .addComponent(userSelection) + .addComponent(enterButton)) + ); + + groupLayout.setHorizontalGroup( + groupLayout.createParallelGroup() + .addGroup( + groupLayout.createSequentialGroup() + .addComponent(label) + .addComponent(gplButton) + .addComponent(infoButton)) + .addGroup( + groupLayout.createSequentialGroup() + .addGroup(groupLayout.createParallelGroup() + .addComponent(workspace) + .addComponent(username)) + .addGroup(groupLayout.createParallelGroup() + .addComponent(workspaceSelection) + .addComponent(userSelection)) + .addGroup(groupLayout.createParallelGroup() + .addComponent(openWorkspaceSettingsBtn) + .addComponent(enterButton)) + ) + ); + } + + @Override + protected void initEventListeners() + { workspaceSelection.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { String workspace = (String) workspaceSelection.getSelectedItem(); @@ -115,7 +243,6 @@ public class WelcomeScreen extends JFrame { }); // Select user combo box - populateUserList(); final JTextComponent tc = (JTextComponent) userSelection.getEditor().getEditorComponent(); tc.getDocument().addDocumentListener(new DocumentListener() { public void removeUpdate(DocumentEvent arg0) { enableOrDisableEnter(); } @@ -155,29 +282,6 @@ public class WelcomeScreen extends JFrame { showInfo(); } }); - - // Add all - initLayout(); - getContentPane().add(panel); - setText(); - pack(); - setVisible(true); - - /* - * Make this component the parent for popups and dialogs. - */ - setMessageManagerParent(); - - workspaceSettings = new WorkspaceSettings(new ActionListener() { - public void actionPerformed(ActionEvent arg0) { - // When the window is closed, repopulate workspace and user lists, update language, enable this window again. - setMessageManagerParent(); - setText(); - populateWorkspaceList(); - populateUserList(); - setEnabled(true); - } - }); } private void initWorkspaceListModel() @@ -226,81 +330,11 @@ public class WelcomeScreen extends JFrame { populateUserList(); } catch (IOException e) { DialogMessenger.getInstance().dispatchMessage( - Logo.messages.getString("ws.error.title"), - Logo.messages.getString("ws.settings.could.not.enter.wp") + "\n\n" + e.toString()); + translate("ws.error.title"), + translate("ws.settings.could.not.enter.wp") + "\n\n" + e.toString()); } } - - private void initLayout() - { - setResizable(false); - infoButton.setBorder(null); - gplButton.setBorder(null); - - infoButton.setOpaque(false); - gplButton.setOpaque(false); - - panel.add(workspace); - panel.add(username); - panel.add(workspaceSelection); - panel.add(userSelection); - panel.add(openWorkspaceSettingsBtn); - panel.add(enterButton); - panel.add(infoButton); - panel.add(gplButton); - - workspaceSelection.setMinimumSize(new Dimension(200, 25)); - userSelection.setMinimumSize(new Dimension(200, 25)); - workspaceSelection.setMaximumSize(new Dimension(200, 25)); - userSelection.setMaximumSize(new Dimension(200, 25)); - - groupLayout = new GroupLayout(panel); - panel.setLayout(groupLayout); - - groupLayout.setAutoCreateGaps(true); - groupLayout.setAutoCreateContainerGaps(true); - - groupLayout.setVerticalGroup( - groupLayout.createSequentialGroup() - .addGroup(groupLayout.createParallelGroup() - .addComponent(gplButton) - .addComponent(infoButton) - .addComponent(label)) - .addGroup(groupLayout.createParallelGroup() - .addComponent(workspace) - .addComponent(workspaceSelection) - .addComponent(openWorkspaceSettingsBtn)) - .addGroup(groupLayout.createParallelGroup() - .addComponent(username) - .addComponent(userSelection) - .addComponent(enterButton)) - ); - - groupLayout.setHorizontalGroup( - groupLayout.createParallelGroup() - .addGroup( - groupLayout.createSequentialGroup() - .addComponent(label) - .addComponent(gplButton) - .addComponent(infoButton)) - .addGroup( - groupLayout.createSequentialGroup() - .addGroup(groupLayout.createParallelGroup() - .addComponent(workspace) - .addComponent(username)) - .addGroup(groupLayout.createParallelGroup() - .addComponent(workspaceSelection) - .addComponent(userSelection)) - .addGroup(groupLayout.createParallelGroup() - .addComponent(openWorkspaceSettingsBtn) - .addComponent(enterButton)) - ) - ); - } - /** - * Display {@link xlogo.gui.welcome.WelcomeScreen} when starting the application. - */ private synchronized void showWorkspaceSettings() { Runnable runnable = new Runnable() { @@ -317,13 +351,13 @@ public class WelcomeScreen extends JFrame { { // Could not authenticate => cancel DialogMessenger.getInstance().dispatchMessage( - Logo.messages.getString("i.am.sorry"), - Logo.messages.getString("welcome.wrong.pw")); + translate("i.am.sorry"), + translate("welcome.wrong.pw")); return; } } - setEnabled(false); + frame.setEnabled(false); workspaceSettings.showFrame(authentification); } }; @@ -331,41 +365,9 @@ public class WelcomeScreen extends JFrame { new Thread(runnable).start(); } - private void setMessageManagerParent() - { - DialogMessenger.getInstance().setMedium(new AsyncMediumAdapter<AsyncMessage<JFrame>, JFrame>(){ - public boolean isReady() - { - return getThis().isDisplayable(); - } - public JFrame getMedium() - { - return getThis(); - } - public void addMediumReadyListener(final AsyncMessenger messenger) - { - getThis().addWindowStateListener(new WindowStateListener(){ - - @Override - public void windowStateChanged(WindowEvent e) - { - if (getThis().isDisplayable()) - messenger.onMediumReady(); - } - }); - } - }); - } - - private JFrame getThis() - { - return this; - } - - protected String showPasswordPopup() { JPasswordField passwordField = new JPasswordField(); - int option = JOptionPane.showConfirmDialog(this, passwordField, Logo.messages.getString("welcome.enter.pw"), + int option = JOptionPane.showConfirmDialog(frame, passwordField, translate("welcome.enter.pw"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if (option == JOptionPane.OK_OPTION) { @@ -384,8 +386,8 @@ public class WelcomeScreen extends JFrame { if(!Storable.checkLegalName(username)) { DialogMessenger.getInstance().dispatchError( - Logo.messages.getString(MessageKeys.NAME_ERROR_TITLE), - Logo.messages.getString(MessageKeys.ILLEGAL_NAME)); + translate(MessageKeys.NAME_ERROR_TITLE), + translate(MessageKeys.ILLEGAL_NAME)); return; } @@ -398,38 +400,24 @@ public class WelcomeScreen extends JFrame { WSManager.getInstance().enterUserSpace(username); } catch (IOException e) { DialogMessenger.getInstance().dispatchMessage( - Logo.messages.getString("ws.error.title"), - Logo.messages.getString("welcome.could.not.enter.user") + e.toString()); + translate("ws.error.title"), + translate("welcome.could.not.enter.user") + e.toString()); return; } + workspaceSettings.stopEventListeners(); onApplicationEnterListener.actionPerformed(new ActionEvent(this, 0, null)); System.gc(); } - @Override - public void dispose() - { - try { - WSManager.getInstance().getGlobalConfigInstance().store(); - } catch (IOException e) { - DialogMessenger.getInstance().dispatchMessage( - Logo.messages.getString("ws.error.title"), - Logo.messages.getString("storage.could.not.store.gc")); - } - - System.gc(); - super.dispose(); - } - public void setText() { - workspace.setText(Logo.messages.getString("welcome.workspace")); - username.setText(Logo.messages.getString("welcome.username")); - openWorkspaceSettingsBtn.setText(Logo.messages.getString("welcome.settings")); - enterButton.setText(Logo.messages.getString("welcome.enter")); - setTitle(Logo.messages.getString("welcome.title")); - pack(); + workspace.setText(translate("welcome.workspace")); + username.setText(translate("welcome.username")); + openWorkspaceSettingsBtn.setText(translate("welcome.settings")); + enterButton.setText(translate("welcome.enter")); + frame.setTitle(translate("welcome.title")); + frame.pack(); } /** @@ -438,7 +426,7 @@ public class WelcomeScreen extends JFrame { */ private void showGPL() { - JFrame frame = new JFrame(Logo.messages.getString("menu.help.licence")); + JFrame frame = new JFrame(translate("menu.help.licence")); frame.setIconImage(Toolkit.getDefaultToolkit().createImage(WebPage.class.getResource("Logo_xlogo4schools.png"))); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.setSize(500, 500); @@ -477,7 +465,7 @@ public class WelcomeScreen extends JFrame { private void showInfo() { - JFrame frame = new JFrame(Logo.messages.getString("menu.help.licence")); + JFrame frame = new JFrame(translate("menu.help.licence")); frame.setIconImage(Toolkit.getDefaultToolkit().createImage(WebPage.class.getResource("Icon_x4s.png"))); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.setSize(500, 500); diff --git a/logo/src/xlogo/gui/welcome/WorkspaceSettings.java b/logo/src/xlogo/gui/welcome/WorkspaceSettings.java index e82acef..e924809 100644 --- a/logo/src/xlogo/gui/welcome/WorkspaceSettings.java +++ b/logo/src/xlogo/gui/welcome/WorkspaceSettings.java @@ -29,8 +29,6 @@ package xlogo.gui.welcome; import java.awt.Toolkit;
import java.awt.event.ActionListener;
-import java.awt.event.WindowEvent;
-import java.awt.event.WindowStateListener;
import java.io.IOException;
import javax.swing.JFrame;
@@ -41,9 +39,6 @@ import xlogo.gui.welcome.settings.tabs.SyntaxHighlightingTab; import xlogo.gui.welcome.settings.tabs.ContestTab;
import xlogo.gui.welcome.settings.tabs.GlobalTab;
import xlogo.gui.welcome.settings.tabs.WorkspaceTab;
-import xlogo.messages.async.AsyncMediumAdapter;
-import xlogo.messages.async.AsyncMessage;
-import xlogo.messages.async.AsyncMessenger;
import xlogo.messages.async.dialog.DialogMessenger;
import xlogo.storage.WSManager;
import xlogo.utils.Utils;
@@ -157,31 +152,5 @@ public class WorkspaceSettings extends X4SFrame { appearanceTab.stopEventListeners();
contestTab.stopEventListeners();
}
-
- private void setMessageManagerParent()
- {
- DialogMessenger.getInstance().setMedium(new AsyncMediumAdapter<AsyncMessage<JFrame>, JFrame>(){
- public boolean isReady()
- {
- return frame.isDisplayable();
- }
- public JFrame getMedium()
- {
- return frame;
- }
- public void addMediumReadyListener(final AsyncMessenger messenger)
- {
- frame.addWindowStateListener(new WindowStateListener(){
-
- @Override
- public void windowStateChanged(WindowEvent e)
- {
- if (frame.isDisplayable())
- messenger.onMediumReady();
- }
- });
- }
- });
- }
-
+
}
|