001 /*
002 Copyright (c) 2009 Olivier Chafik, All Rights Reserved
003
004 This file is part of JNAerator (http://jnaerator.googlecode.com/).
005
006 JNAerator is free software: you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation, either version 3 of the License, or
009 (at your option) any later version.
010
011 JNAerator is distributed in the hope that it will be useful,
012 but WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 GNU General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with JNAerator. If not, see <http://www.gnu.org/licenses/>.
018 */
019 package com.ochafik.swing;
020
021 import java.awt.BorderLayout;
022 import java.awt.Component;
023 import java.awt.GridBagConstraints;
024 import java.awt.GridBagLayout;
025 import java.awt.Insets;
026 import java.awt.event.ActionEvent;
027 import java.beans.PropertyChangeEvent;
028 import java.beans.PropertyChangeListener;
029
030 import javax.swing.AbstractAction;
031 import javax.swing.Action;
032 import javax.swing.ActionMap;
033 import javax.swing.Box;
034 import javax.swing.InputMap;
035 import javax.swing.JComponent;
036 import javax.swing.JLabel;
037 import javax.swing.JPanel;
038 import javax.swing.KeyStroke;
039 import javax.swing.text.JTextComponent;
040 import javax.swing.undo.UndoManager;
041
042 import com.ochafik.beans.BeansController;
043
044
045 public class FormUtils {
046
047 @SuppressWarnings("serial")
048 public static Action createUndoAction(final UndoManager undoManager, String name) {
049 return new AbstractAction(name) {
050 public void actionPerformed(ActionEvent arg0) {
051 if (undoManager.canUndo()) {
052 undoManager.undo();
053 }
054 }
055 };
056 }
057 @SuppressWarnings("serial")
058 public static Action createRedoAction(final UndoManager undoManager, String name) {
059 return new AbstractAction(name) {
060 public void actionPerformed(ActionEvent arg0) {
061 if (undoManager.canRedo()) {
062 undoManager.redo();
063 }
064 }
065 };
066 }
067 public static void registerUndoRedoActions(JComponent jtc, Action undoAction, Action redoAction) {
068 InputMap inputMap = jtc.getInputMap();
069 inputMap.put(KeyStroke.getKeyStroke("pressed UNDO"), "undo");
070 inputMap.put(KeyStroke.getKeyStroke("ctrl pressed Z"), "undo");
071 inputMap.put(KeyStroke.getKeyStroke("meta pressed Z"), "undo");
072
073 inputMap.put(KeyStroke.getKeyStroke("pressed REDO"), "redo");
074 inputMap.put(KeyStroke.getKeyStroke("ctrl pressed Y"), "redo");
075 inputMap.put(KeyStroke.getKeyStroke("meta pressed Y"), "redo");
076
077 ActionMap actionMap = jtc.getActionMap();
078 actionMap.put("undo", undoAction);
079 actionMap.put("redo", redoAction);
080 }
081
082 public final static void addUndoRedoSupport(final JTextComponent jtc) {
083 jtc.addPropertyChangeListener("document", new PropertyChangeListener() {
084 public void propertyChange(PropertyChangeEvent evt) {
085 UndoRedoUtils.registerNewUndoManager(jtc);
086 }
087
088 });
089 UndoRedoUtils.registerNewUndoManager(jtc);
090 }
091 public final static JPanel makeEntriesPanel(BeansController<?> beansController,int widthMin,FormElement[] formElements) {
092 GridBagLayout gbl = new GridBagLayout();
093 GridBagConstraints gbc = new GridBagConstraints();
094 gbc.weighty=1;
095
096 JPanel panel=new JPanel(gbl);//new GridLayout(captionsAndProperties.length,2));
097 int space=5,pad=1;
098 Insets labelsInsets=new Insets(pad,space,pad,space);
099 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 }