?? swingbuilder.java
字號:
/*
$Id: SwingBuilder.java 4247 2006-11-19 19:00:19Z mcspanky $
Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
Redistribution and use of this software and associated documentation
("Software"), with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain copyright
statements and notices. Redistributions must also contain a
copy of this document.
2. Redistributions in binary form must reproduce the
above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
3. The name "groovy" must not be used to endorse or promote
products derived from this Software without prior written
permission of The Codehaus. For written permission,
please contact info@codehaus.org.
4. Products derived from this Software may not be called "groovy"
nor may "groovy" appear in their names without prior written
permission of The Codehaus. "groovy" is a registered
trademark of The Codehaus.
5. Due credit should be given to The Codehaus -
http://groovy.codehaus.org/
THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package groovy.swing;
import groovy.lang.Closure;
import groovy.lang.MissingMethodException;
import groovy.model.DefaultTableModel;
import groovy.model.ValueHolder;
import groovy.model.ValueModel;
import groovy.swing.impl.ComponentFacade;
import groovy.swing.impl.ContainerFacade;
import groovy.swing.impl.DefaultAction;
import groovy.swing.impl.Factory;
import groovy.swing.impl.Startable;
import groovy.swing.impl.TableLayout;
import groovy.swing.impl.TableLayoutCell;
import groovy.swing.impl.TableLayoutRow;
import groovy.util.BuilderSupport;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.Window;
import java.text.Format;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.DefaultBoundedRangeModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JPopupMenu;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.JToolTip;
import javax.swing.JTree;
import javax.swing.JViewport;
import javax.swing.JWindow;
import javax.swing.KeyStroke;
import javax.swing.OverlayLayout;
import javax.swing.RootPaneContainer;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SpringLayout;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import org.codehaus.groovy.runtime.InvokerHelper;
/**
* A helper class for creating Swing widgets using GroovyMarkup
*
* @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
* @version $Revision: 4247 $
*/
public class SwingBuilder extends BuilderSupport {
private Logger log = Logger.getLogger(getClass().getName());
private Map factories = new HashMap();
private Object constraints;
private Map passThroughNodes = new HashMap();
private Map widgets = new HashMap();
// tracks all containing windows, for auto-owned dialogs
private LinkedList containingWindows = new LinkedList();
public SwingBuilder() {
registerWidgets();
}
public Object getProperty(String name) {
Object widget = widgets.get(name);
if (widget == null) {
return super.getProperty(name);
}
return widget;
}
protected void setParent(Object parent, Object child) {
if (child instanceof Action) {
Action action = (Action) child;
try {
InvokerHelper.setProperty(parent, "action", action);
} catch (RuntimeException re) {
// must not have an action property...
// so we ignore it and go on
}
Object keyStroke = action.getValue("KeyStroke");
//System.out.println("keystroke: " + keyStroke + " for: " + action);
if (parent instanceof JComponent) {
JComponent component = (JComponent) parent;
KeyStroke stroke = null;
if (keyStroke instanceof String) {
stroke = KeyStroke.getKeyStroke((String) keyStroke);
}
else if (keyStroke instanceof KeyStroke) {
stroke = (KeyStroke) keyStroke;
}
if (stroke != null) {
String key = action.toString();
component.getInputMap().put(stroke, key);
component.getActionMap().put(key, action);
}
}
}
else if (child instanceof LayoutManager) {
if (parent instanceof RootPaneContainer) {
RootPaneContainer rpc = (RootPaneContainer) parent;
parent = rpc.getContentPane();
}
InvokerHelper.setProperty(parent, "layout", child);
}
else if (child instanceof JToolTip && parent instanceof JComponent) {
((JToolTip)child).setComponent((JComponent)parent);
}
else if (parent instanceof JTable && child instanceof TableColumn) {
JTable table = (JTable) parent;
TableColumn column = (TableColumn) child;
table.addColumn(column);
}
else if (parent instanceof JTabbedPane && child instanceof Component) {
JTabbedPane tabbedPane = (JTabbedPane) parent;
tabbedPane.add((Component)child);
}
else if (child instanceof Window) {
// do nothing. owner of window is set elsewhere, and this
// shouldn't get added to any parent as a child
// if it is a top level component anyway
}
else {
Component component = null;
if (child instanceof Component) {
component = (Component) child;
}
else if (child instanceof ComponentFacade) {
ComponentFacade facade = (ComponentFacade) child;
component = facade.getComponent();
}
if (component != null) {
if (parent instanceof JFrame && component instanceof JMenuBar) {
JFrame frame = (JFrame) parent;
frame.setJMenuBar((JMenuBar) component);
}
else if (parent instanceof RootPaneContainer) {
RootPaneContainer rpc = (RootPaneContainer) parent;
if (constraints != null) {
rpc.getContentPane().add(component, constraints);
} else {
rpc.getContentPane().add(component);
}
}
else if (parent instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane) parent;
if (child instanceof JViewport) {
scrollPane.setViewport((JViewport)component);
}
else {
scrollPane.setViewportView(component);
}
}
else if (parent instanceof JSplitPane) {
JSplitPane splitPane = (JSplitPane) parent;
if (splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
if (splitPane.getTopComponent() == null) {
splitPane.setTopComponent(component);
}
else {
splitPane.setBottomComponent(component);
}
}
else {
if (splitPane.getLeftComponent() == null) {
splitPane.setLeftComponent(component);
}
else {
splitPane.setRightComponent(component);
}
}
}
else if (parent instanceof JMenuBar && component instanceof JMenu) {
JMenuBar menuBar = (JMenuBar) parent;
menuBar.add((JMenu) component);
}
else if (parent instanceof Container) {
Container container = (Container) parent;
if (constraints != null) {
container.add(component, constraints);
}
else {
container.add(component);
}
}
else if (parent instanceof ContainerFacade) {
ContainerFacade facade = (ContainerFacade) parent;
facade.addComponent(component);
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -