View Javadoc

1   /*
2   	Copyright (c) 2009 Olivier Chafik, All Rights Reserved
3   	
4   	This file is part of JNAerator (http://jnaerator.googlecode.com/).
5   	
6   	JNAerator is free software: you can redistribute it and/or modify
7   	it under the terms of the GNU General Public License as published by
8   	the Free Software Foundation, either version 3 of the License, or
9   	(at your option) any later version.
10  	
11  	JNAerator is distributed in the hope that it will be useful,
12  	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  	GNU General Public License for more details.
15  	
16  	You should have received a copy of the GNU General Public License
17  	along with JNAerator.  If not, see <http://www.gnu.org/licenses/>.
18  */
19  package com.ochafik.swing;
20  
21  import java.awt.BorderLayout;
22  import java.awt.Component;
23  import java.awt.GridBagConstraints;
24  import java.awt.GridBagLayout;
25  import java.awt.Insets;
26  import java.awt.event.ActionEvent;
27  import java.beans.PropertyChangeEvent;
28  import java.beans.PropertyChangeListener;
29  
30  import javax.swing.AbstractAction;
31  import javax.swing.Action;
32  import javax.swing.ActionMap;
33  import javax.swing.Box;
34  import javax.swing.InputMap;
35  import javax.swing.JComponent;
36  import javax.swing.JLabel;
37  import javax.swing.JPanel;
38  import javax.swing.KeyStroke;
39  import javax.swing.text.JTextComponent;
40  import javax.swing.undo.UndoManager;
41  
42  import com.ochafik.beans.BeansController;
43  
44  
45  public class FormUtils {
46  	
47  	@SuppressWarnings("serial")
48  	public static Action createUndoAction(final UndoManager undoManager, String name) {
49  		return new AbstractAction(name) {
50              public void actionPerformed(ActionEvent arg0) {
51                  if (undoManager.canUndo()) {
52                      undoManager.undo();
53                  }
54              }
55          };
56  	}
57  	@SuppressWarnings("serial")
58  	public static Action createRedoAction(final UndoManager undoManager, String name) {
59  		return new AbstractAction(name) {
60              public void actionPerformed(ActionEvent arg0) {
61                  if (undoManager.canRedo()) {
62                      undoManager.redo();
63                  }
64              }
65          };
66  	}
67  	public static void registerUndoRedoActions(JComponent jtc, Action undoAction, Action redoAction) {
68          InputMap inputMap = jtc.getInputMap();
69          inputMap.put(KeyStroke.getKeyStroke("pressed UNDO"), "undo");
70          inputMap.put(KeyStroke.getKeyStroke("ctrl pressed Z"), "undo");
71          inputMap.put(KeyStroke.getKeyStroke("meta pressed Z"), "undo");
72          
73          inputMap.put(KeyStroke.getKeyStroke("pressed REDO"), "redo");
74          inputMap.put(KeyStroke.getKeyStroke("ctrl pressed Y"), "redo");
75          inputMap.put(KeyStroke.getKeyStroke("meta pressed Y"), "redo");
76          
77          ActionMap actionMap = jtc.getActionMap();
78          actionMap.put("undo", undoAction);
79          actionMap.put("redo", redoAction);
80  	}
81  	
82      public final static void addUndoRedoSupport(final JTextComponent jtc) {
83      	jtc.addPropertyChangeListener("document", new PropertyChangeListener() {
84      		public void propertyChange(PropertyChangeEvent evt) {
85      			UndoRedoUtils.registerNewUndoManager(jtc);
86      		}
87      		
88      	});
89      	UndoRedoUtils.registerNewUndoManager(jtc);
90      }
91      public final static JPanel makeEntriesPanel(BeansController<?> beansController,int widthMin,FormElement[] formElements)  {
92          GridBagLayout gbl = new GridBagLayout();
93          GridBagConstraints gbc = new GridBagConstraints();
94          gbc.weighty=1;
95          
96          JPanel panel=new JPanel(gbl);//new GridLayout(captionsAndProperties.length,2));
97          int space=5,pad=1;
98          Insets labelsInsets=new Insets(pad,space,pad,space);
99          Insets editorsInsets=new Insets(pad,pad,pad,pad);
100         for (int i=0;i<formElements.length;i++) {
101             gbc.gridy=i;
102             
103             FormElement formElement=formElements[i];
104             
105             JLabel label=new JLabel(formElement.name,JLabel.RIGHT);
106             gbc.fill=GridBagConstraints.NONE;
107             gbc.anchor=GridBagConstraints.NORTHEAST;
108             gbc.gridx=0;
109             gbc.weightx=0;
110             gbc.insets=labelsInsets;
111             gbl.setConstraints(label, gbc);
112             panel.add(label);
113             
114             Component editor=
115                 formElement.editorComponent==null ?
116                     beansController.createScrollableViewComponent(
117                         formElement.propertyName, 
118                         null, 
119                         formElement.title, 
120                         formElement.toolTip, 
121                         formElement.largeComponent
122                     ) :
123                         formElement.editorComponent;
124             gbc.fill=GridBagConstraints.HORIZONTAL;
125             gbc.gridx=1;
126             gbc.weightx=6;
127             gbc.insets=editorsInsets;
128             gbl.setConstraints(editor, gbc);
129             panel.add(editor);
130         }
131         JPanel ret=new JPanel(new BorderLayout());
132         ret.add("Center",panel);
133         ret.add("South",Box.createHorizontalStrut(widthMin));
134         return ret;
135     }
136 }