001 /*
002 * Copyright (c) 1995-2010, The University of Sheffield. See the file
003 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004 *
005 * This file is part of GATE (see http://gate.ac.uk/), and is free
006 * software, licenced under the GNU Library General Public License,
007 * Version 2, June 1991 (in the distribution as file licence.html,
008 * and also available at http://gate.ac.uk/gate/licence.html).
009 *
010 * Valentin Tablan 16/10/2001
011 *
012 * $Id: FeatureMapEditorDialog.java 13586 2011-03-30 11:56:36Z markagreenwood $
013 *
014 */
015
016 package gate.gui;
017
018 import java.awt.BorderLayout;
019 import java.awt.Component;
020 import java.awt.Dimension;
021 import java.awt.event.ActionEvent;
022 import java.awt.event.ActionListener;
023 import java.util.*;
024
025 import javax.swing.*;
026
027 import gate.Factory;
028 import gate.FeatureMap;
029 import gate.Gate;
030 import gate.creole.ResourceData;
031 import gate.creole.ResourceInstantiationException;
032 import gate.util.*;
033
034 /**
035 * A simple editor for List values.
036 */
037 public class FeatureMapEditorDialog extends JDialog {
038
039 /**
040 * Contructs a new FeatureMapEditorDialog.
041 *
042 * @param owner the component this dialog will be centred on.
043 * @param data a feature map with the initial values. This map will
044 * not be changed, its values will be cached and if the user
045 * selects the OK option a new map with the updated contents
046 * will be returned.
047 */
048 public FeatureMapEditorDialog(Component owner, FeatureMap data) {
049 super(MainFrame.getInstance());
050 setLocationRelativeTo(owner);
051 initLocalData(data);
052 initGuiComponents();
053 initListeners();
054 }
055
056 protected void initLocalData(FeatureMap data) {
057 if(data != null) {
058 FeatureMap fm = Factory.newFeatureMap();
059 fm.putAll(data);
060 tempFMHolder.setFeatures(fm);
061 }
062 }
063
064 protected void initGuiComponents() {
065 this.setMinimumSize(new Dimension(150, 300));
066 getContentPane().setLayout(new BorderLayout());
067
068 // create the FeaturesSchemaEditor for the main body of the dialog
069 fmView = new FeaturesSchemaEditor();
070 try {
071 fmView.init();
072 }
073 catch(ResourceInstantiationException rie) {
074 // can't happen, but needs to be caught to satisfy the compiler
075 throw new LuckyException("FeaturesSchemaEditor.init() threw "
076 + "ResourceInstantiationException!");
077 }
078 fmView.setTarget(tempFMHolder);
079 // make sure the window is a sensible size
080 Dimension preferredSize = fmView.getPreferredSize();
081 if(preferredSize.height < 150) {
082 preferredSize.height = 150;
083 }
084 else if(preferredSize.height > 300) {
085 preferredSize.height = 300;
086 }
087 fmView.setPreferredSize(preferredSize);
088 JPanel fmViewPanel = new JPanel(new BorderLayout());
089 fmViewPanel.add(fmView, BorderLayout.CENTER);
090 fmViewPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
091
092 getContentPane().add(fmViewPanel, BorderLayout.CENTER);
093
094 // the bottom buttons
095 Box buttonsBox = Box.createHorizontalBox();
096 buttonsBox.add(Box.createHorizontalGlue());
097 okButton = new JButton("OK");
098 buttonsBox.add(okButton);
099 buttonsBox.add(Box.createHorizontalStrut(5));
100 cancelButton = new JButton("Cancel");
101 buttonsBox.add(cancelButton);
102 buttonsBox.add(Box.createHorizontalGlue());
103 getContentPane().add(buttonsBox, BorderLayout.SOUTH);
104 }
105
106 protected void initListeners() {
107 okButton.addActionListener(new ActionListener() {
108 public void actionPerformed(ActionEvent e) {
109 userCancelled = false;
110 setVisible(false);
111 }
112 });
113
114 cancelButton.addActionListener(new ActionListener() {
115 public void actionPerformed(ActionEvent e) {
116 userCancelled = true;
117 setVisible(false);
118 }
119 });
120 }
121
122 /**
123 * Make this dialog visible allowing the editing of the list. If the
124 * user selects the <b>OK</b> option a new list with the updated
125 * contents will be returned; it the <b>Cancel</b> option is selected
126 * this method return <tt>null</tt>.
127 */
128 public FeatureMap showDialog() {
129 pack();
130 userCancelled = true;
131 setModal(true);
132 super.setVisible(true);
133 return userCancelled ? null : tempFMHolder.getFeatures();
134 }
135
136 /**
137 * test code
138 */
139 public static void main(String[] args) {
140 try {
141 Gate.init();
142 }
143 catch(Exception e) {
144 e.printStackTrace();
145 }
146 JFrame frame = new JFrame("Foo frame");
147
148 FeatureMapEditorDialog dialog = new FeatureMapEditorDialog(frame, null);
149
150 frame.setSize(300, 300);
151 frame.setVisible(true);
152 System.out.println(dialog.showDialog());
153 }
154
155 /**
156 * A dummy FeatureBearer used to hold the temporary feature map used
157 * for the editor.
158 */
159 FeatureBearer tempFMHolder = new FeatureBearer() {
160 private FeatureMap fm;
161
162 public void setFeatures(FeatureMap map) {
163 fm = map;
164 }
165
166 public FeatureMap getFeatures() {
167 return fm;
168 }
169 };
170
171 /**
172 * The GUI compoenent used to display the feature map.
173 */
174 FeaturesSchemaEditor fmView;
175
176 /**
177 * The OK button for this dialog
178 */
179 JButton okButton;
180
181 /**
182 * The cancel button for this dialog
183 */
184 JButton cancelButton;
185
186 /**
187 * Did the user press the cancel button?
188 */
189 boolean userCancelled;
190 }
|