Skip to content

Instantly share code, notes, and snippets.

@dulichan
Created March 29, 2013 04:28
Show Gist options
  • Save dulichan/5268756 to your computer and use it in GitHub Desktop.
Save dulichan/5268756 to your computer and use it in GitHub Desktop.
Swing action component framework
/**
* Copyright (c) 2013 Dulitha Wijewantha (http://www.dulithawijewantha.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.dchan.ui;
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.dchan.context.Context;
import org.dchan.context.SessionContextManager;
/**
* {@link ActionHelper} is a class that maps out as a base for the Actions that are
* later developed. This class uses template design pattern to full fill the
* design requirements. An action's life cycle can be best described as bellow
*
* <ul>
* <li>Instantiated from the View's constructor</li>
*
* <li>Routes to the ActionHelper base from call to super</li>
*
* <li>Variables will be initialized</li>
*
* <li>Context will be created and Swing Components will be discovered</li>
*
* <li>Process method will execute</li>
*
* <li>Variables of implemented actions's will be initialized</li>
*
* <li>Controllers of implemented action's will be initialized</li>
* </ul>
*
* @author Chan (Dulitha R. Wijewantha [email protected])
* @version 1.2
*
*/
public abstract class ActionHelper {
/**
* This contentPanel could be any component that extends JComponent and this
* includes JPanel, JLayeredPanel
*/
private JComponent contentPanel;
LookupHelper lookupHelper;
Context context;
/**
* Constructor for ActionHelper.
*
* @param contentPanel
* JComponent
* @param contextName
* String
*/
public ActionHelper(JComponent contentPanel, String contextName) {
this.contentPanel = contentPanel;
this.lookupHelper = new LookupHelper(contentPanel.getComponents());
this.context = SessionContextManager.getInstance().getContext(
contextName);
process();
}
/**
* This method is to be overridden and this consists the actions that will
* occur in the view
*/
public abstract void executeAction();
/**
* This method is intended to be overridden. Initialization of controllers
* would occur in this method
*/
public abstract void initializeControllers();
/**
* This method is used to initialize variables for the constructor
*/
public abstract void initilaizeVariables();
/**
* This is the template method that is executed once the constructor is
* complete
*/
public void process() {
initilaizeVariables();
initializeControllers();
executeAction();
}
/**
*
* @return The Lookup helper to process over the Components
*/
public LookupHelper getLookupHelper() {
return lookupHelper;
}
/**
* This is a wrapper method over the Lookup engine
*
* @param s
* - String
*
* @return Component
*/
public Component get(String s) {
return lookupHelper.get(s);
}
/**
* Below is a set of methods to ease up getting components out of the Tree
*
* @return Context
*/
public Context getContext() {
return context;
}
/**
* Method getCombox.
*
* @param s
* String
* @return JComboBox
*/
public JComboBox getCombox(String s) {
return (JComboBox) lookupHelper.get(s);
}
/**
* Method getTable.
*
* @param s
* String
* @return JTable
*/
public JTable getTable(String s) {
return (JTable) lookupHelper.get(s);
}
/**
* Method getTextField.
*
* @param s
* String
* @return JTextField
*/
public JTextField getTextField(String s) {
return (JTextField) lookupHelper.get(s);
}
/**
* Method getTextArea.
*
* @param s
* String
* @return JTextArea
*/
public JTextArea getTextArea(String s) {
return (JTextArea) lookupHelper.get(s);
}
/**
* Method getButton.
*
* @param s
* String
* @return JButton
*/
public JButton getButton(String s) {
return (JButton) lookupHelper.get(s);
}
/**
* Method getPanel.
*
* @param string
* String
* @return JPanel
*/
public JPanel getPanel(String string) {
return (JPanel) lookupHelper.get(string);
}
/**
* Method getFormattedTextField.
*
* @param name
* String
* @return JFormattedTextField
*/
public JFormattedTextField getFormattedTextField(String name) {
return (JFormattedTextField) get(name);
}
/**
* Method getPasswordField.
*
* @param name
* String
* @return JPasswordField
*/
public JPasswordField getPasswordField(String name) {
return (JPasswordField) get(name);
}
/**
* Method getTabbedPane.
*
* @param name
* String
* @return JTabbedPane
*/
public JTabbedPane getTabbedPane(String name) {
return (JTabbedPane) get(name);
}
public JRadioButton getRadioButton(String name) {
return (JRadioButton) get(name);
}
public JComponent getContentPanel() {
return contentPanel;
}
public JCheckBox getCheckBox(String name) {
return (JCheckBox) get(name);
}
public JLabel getLabel(String name) {
return (JLabel) get(name);
}
public JEditorPane getEditorPane(String name) {
return (JEditorPane) get(name);
}
}
/**
* Copyright (c) 2013 Dulitha Wijewantha (http://www.dulithawijewantha.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.dchan.ui;
import java.awt.CardLayout;
import javax.swing.JPanel;
/**
* Basic wrapper used to switch Cards in a {@link CardLayout}
*
* @author Chan (Dulitha R. Wijewantha [email protected])
* @version 1.2
*/
public class CardLayoutHelper {
private JPanel panel;
private CardLayout layout;
/**
*
*
* @param panel
* JPanel
*/
public CardLayoutHelper(JPanel panel) {
this.panel = panel;
this.layout = (CardLayout) this.panel.getLayout();
}
public CardLayoutHelper(JPanel panel, JPanel... panels) {
this(panel);
for (int i = 0; i < panels.length; i++) {
JPanel jPanel = panels[i];
panel.add(jPanel.getName(), jPanel);
}
}
/**
* All the panels will be removed and the sent {@link JPanel} will be
* rendered
*
* @param currentPanel
* - The panel that will be switched into the view
*/
public void switchPanel(JPanel currentPanel) {
panel.removeAll();
panel.add(currentPanel, currentPanel.getName());
layout.show(panel, currentPanel.getName());
panel.revalidate();
panel.repaint();
}
/**
* Switch the panel to a previously added {@link JPanel}
*
* @param name
*/
public void switchPanel(String name) {
layout.show(panel, name);
panel.revalidate();
panel.repaint();
}
}
/**
* Copyright (c) 2013 Dulitha Wijewantha (http://www.dulithawijewantha.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.dchan.ui;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
*
* Wrapper class that will support {@link JDialog} to change panels
*
* @author Chan (Dulitha R. Wijewantha [email protected])
* @version 1.2
*/
public class JDialogHelper extends WindowHelper {
JDialog dialog;
/**
* Constructor for JDialogHelper.
*
* @param d
* JDialog
*/
public JDialogHelper(JDialog d) {
dialog = d;
setPanel((JPanel) dialog.getContentPane());
}
/**
* This method is used to switch the Panel of a Dialog box.
*
* @param newPanel
* - The Panel that we need to be visible in the Dialog box
*/
public void switchPanel(final JPanel newPanel) {
super.switchPanel(newPanel);
dialog.setTitle(newPanel.getName());
dialog.repaint();
dialog.pack();
dialog.setVisible(true);
}
}
/**
* Copyright (c) 2013 Dulitha Wijewantha (http://www.dulithawijewantha.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.dchan.ui;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* A wrapper object that is part of the UI stack. It will allow us to bind a
* JFrame -> JDialog.
*
* When it comes to Applications JFrame presents to models of interaction
* <ul>
* <li>Fullscreen</li>
* <li>Non-fullscreen</li>
* </ul>
*
* @author Chan (Dulitha R. Wijewantha [email protected])
* @version 1.2
*/
public class JFrameHelper extends WindowHelper {
JFrame frame;
JDialog dialog;
boolean isFullscreen = false;
/**
* Constructor for JFrameHelper.
*
* @param f
* JFrame
* @param d
* JDialog
* @param panel
* JPanel
*/
public JFrameHelper(JFrame f, JDialog d, JPanel panel) {
frame = f;
dialog = d;
this.setPanel(panel);
}
/**
* Additional Constructor for JFrameHelper that can define
*
* @param f
* JFrame
* @param d
* JDialog
* @param panel
* JPanel
*/
public JFrameHelper(JFrame f, JDialog d, JPanel panel, boolean isFullscreen) {
frame = f;
dialog = d;
this.setPanel(panel);
this.isFullscreen = isFullscreen;
}
/**
* Method switchPanel.
*
* @param newPanel
* JPanel
*/
@Override
public void switchPanel(JPanel newPanel) {
super.switchPanel(newPanel);
frame.setTitle(newPanel.getName());
if (!isFullscreen) {
frame.pack();
}
}
/**
* Hides the {@link JDialog} associated and switch the panel in the
* MainFrame
*
* @param newPanel
* - The panel that will be displayed in the MainFrame
*/
public void hideDialog(JPanel newPanel) {
dialog.setVisible(false);
switchPanel(newPanel);
}
/**
* Hides the {@link JDialog} associated
*/
public void hideDialog() {
dialog.setVisible(false);
}
}
/**
* Copyright (c) 2013 Dulitha Wijewantha (http://www.dulithawijewantha.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.dchan.ui;
import java.awt.Component;
import java.awt.Container;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
/**
*
* {@link LookupHelper} is used as a Utility by {@link ActionHelper} to make
* sure that all the components in the Container is available to
* {@link ActionHelper}. The class will internally analyze all the panels and
* traverse their object hierarchies discovering Swing Components with name
* attribute specified. The discovered attributes will be added to a map to be
* accessed later.
*
* Below are the conventions to be followed when naming Swing Components
* <ul>
* <li>Swing Buttons <strong>btn</strong></li>
* <li>Swing Labels <strong>lbl</strong></li>
* <li>Swing Checkbox<strong>chk</strong></li>
* <li>Swing Tables <strong>tbl</strong></li>
* <li>Swing Comboboxes <strong>cmb</strong></li>
* <li>Swing Panel <strong>pnl</strong></li>
* <li>Swing Textfield <strong>txt</strong></li>
* <li>Swing Textareas <strong>txa</strong></li>
* </ul>
*
* @author Chan (Dulitha R. Wijewantha [email protected])
* @version 1.2
*/
public class LookupHelper {
/**
* Allows execution of custom code
*/
UIHookHelper hookHelper = new UIHookHelper();
/**
* Consists of components that have been traversed by the Component Lookup
* drone
*/
private Map<String, Component> componentTree;
/**
* Constructor takes a Component array which derives from the
* {@link Container}.getComponents();
*
* @param components
* - Primary component List
*/
public LookupHelper(Component[] components) {
componentTree = new HashMap<String, Component>();
for (int i = 0; i < components.length; i++) {
Component component = components[i];
delegate(component);
}
}
/**
* This method specifically takes care of traversing through the JPanel's
* inner components
*
* @param jPanel
*/
private void lookupJPanel(JPanel jPanel) {
Component[] components = jPanel.getComponents();
for (int i = 0; i < components.length; i++) {
Component component = components[i];
delegate(component);
}
}
/**
* This method takes care of traversing through the JScrollPane's inner
* components
*
* @param jScrollPane
*/
private void lookupJScrollPane(JScrollPane jScrollPane) {
Component[] components = jScrollPane.getViewport().getComponents();
for (int i = 0; i < components.length; i++) {
Component component = components[i];
delegate(component);
}
}
/**
* This method takes care of traversing through the JTabbedPane inner
* components
*
* @param jTabbedPane
*/
private void lookupJTabbedPane(JTabbedPane jTabbedPane) {
Component[] components = jTabbedPane.getComponents();
for (int i = 0; i < components.length; i++) {
Component component = components[i];
if (component instanceof JPanel) {
delegate(component);
}
}
}
/**
* Delegates all component discoveries. This method ensures that inner
* components are properly added to the context
*
* @param component
*/
private void delegate(Component component) {
if (component instanceof JPanel) {
lookupJPanel((JPanel) component);
} else if (component instanceof JScrollPane) {
lookupJScrollPane((JScrollPane) component);
} else if (component instanceof JTabbedPane) {
lookupJTabbedPane((JTabbedPane) component);
}
/**
* This object applies UI changes universally
*/
hookHelper.applyHook(component);
getTree().put(component.getName(), component);
}
/**
* This method is general wrapper for the context. Getter can be used to
* obtain components that have not been delegated with methods
*
* @param s
* - Name of the component
*
* @return Component
*/
public Component get(String s) {
return componentTree.get(s);
}
/**
* This method can be used to obtain the whole context. This might be useful
* for more collection related operations
*
*
* @return Map<String,Component>
*/
public Map<String, Component> getTree() {
return componentTree;
}
}
/**
* Copyright (c) 2013 Dulitha Wijewantha (http://www.dulithawijewantha.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.dchan.ui;
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
/**
* Provides universal UI change facility for the Action. All the traversed
* components will be sent to the custom method
*
* @author Chan (Dulitha R. Wijewantha [email protected])
* @version 1.2
*/
public class UIHookHelper {
/**
* Apply UI changes
* Eg:- Font Change
* @param component
*/
public void applyHook(Component component) {
if (component instanceof JTable) {
JTable table = (JTable) component;
} else if (component instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane) component;
} else if (component instanceof JButton) {
JButton button = (JButton) component;
}
}
}
/**
* Copyright (c) 2013 Dulitha Wijewantha (http://www.dulithawijewantha.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.dchan.ui;
import java.awt.CardLayout;
import java.awt.Panel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
* The abstract class supporting the binding of {@link JFrameHelper} and
* {@link JDialogHelper} to the {@link CardLayoutHelper}.
*
* @author Chan (Dulitha R. Wijewantha [email protected])
* @version 1.2
*/
public abstract class WindowHelper {
CardLayout layout;
JPanel panel;
/**
* @deprecated due to the fact that setPanel can Inject the layout with the
* use of the main panel
* @param la
*/
public void setCardLayout(CardLayout la) {
layout = la;
}
/**
* This Method injects the Panel and the CardLayout is obtained via it.
*
* @param pa
* - The Main Panel that has the CardLayout in itself.
*/
public void setPanel(JPanel pa) {
panel = pa;
layout = (CardLayout) pa.getLayout();
}
/**
*
* @param newPanel
* - The Panel that will be swapped with the Panel in the view
* Object. The View object is the object that contains the
* CardLayout
*
* @see Panel should be first injected manually by us to use WindowHelper.
*/
public void switchPanel(final JPanel newPanel) {
panel.removeAll();
panel.add(newPanel, newPanel.getName());
layout.show(panel, newPanel.getName());
panel.revalidate();
panel.repaint();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment