1
|
/* A text editor to edit a string attribute.
|
2
|
|
3
|
Copyright (c) 2003-2006 The Regents of the University of California.
|
4
|
All rights reserved.
|
5
|
Permission is hereby granted, without written agreement and without
|
6
|
license or royalty fees, to use, copy, modify, and distribute this
|
7
|
software and its documentation for any purpose, provided that the above
|
8
|
copyright notice and the following two paragraphs appear in all copies
|
9
|
of this software.
|
10
|
|
11
|
IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
|
12
|
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
13
|
ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
|
14
|
THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
|
15
|
SUCH DAMAGE.
|
16
|
|
17
|
THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
|
18
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
19
|
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
|
20
|
PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
|
21
|
CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
|
22
|
ENHANCEMENTS, OR MODIFICATIONS.
|
23
|
|
24
|
PT_COPYRIGHT_VERSION_2
|
25
|
COPYRIGHTENDKEY
|
26
|
|
27
|
*/
|
28
|
package ptolemy.vergil.toolbox;
|
29
|
|
30
|
import java.awt.event.KeyEvent;
|
31
|
|
32
|
import javax.swing.JOptionPane;
|
33
|
|
34
|
import ptolemy.actor.gui.TextEditor;
|
35
|
import ptolemy.kernel.util.NamedObj;
|
36
|
import ptolemy.kernel.util.StringAttribute;
|
37
|
import ptolemy.moml.MoMLChangeRequest;
|
38
|
import ptolemy.util.StringUtilities;
|
39
|
|
40
|
/**
|
41
|
A text editor to edit a specified string attribute.
|
42
|
|
43
|
@author Edward A. Lee
|
44
|
@version $Id: TextEditorForStringAttributes.java 41477 2006-03-29 00:03:58Z cxh $
|
45
|
@since Ptolemy II 4.0
|
46
|
@Pt.ProposedRating Yellow (eal)
|
47
|
@Pt.AcceptedRating Red (ptolemy)
|
48
|
*/
|
49
|
public class TextEditorForStringAttributes extends TextEditor {
|
50
|
/** Create a annotation text editor for the specified attribute.
|
51
|
* @param factory The factory that created this editor.
|
52
|
* @param attributeToEdit The string attribute to edit.
|
53
|
* @param rows The number of rows.
|
54
|
* @param columns The number of columns.
|
55
|
* @param title The window title to use.
|
56
|
*/
|
57
|
public TextEditorForStringAttributes(TextEditorFactory factory,
|
58
|
StringAttribute attributeToEdit, int rows, int columns, String title) {
|
59
|
super(title);
|
60
|
this._factory = factory;
|
61
|
_attributeToEdit = attributeToEdit;
|
62
|
text.append(_attributeToEdit.getExpression());
|
63
|
text.setColumns(columns);
|
64
|
text.setRows(rows);
|
65
|
|
66
|
_originalText = _attributeToEdit.getExpression();
|
67
|
_currentText = _originalText;
|
68
|
_wasModified = false; // initialize to false
|
69
|
|
70
|
// The above will mark the text object modified. Reverse this.
|
71
|
setModified(false);
|
72
|
}
|
73
|
|
74
|
///////////////////////////////////////////////////////////////////
|
75
|
//// public methods ////
|
76
|
|
77
|
/** Adjust the file menu so that only relevant items appear.
|
78
|
* This has to be called after pack().
|
79
|
*/
|
80
|
public void adjustFileMenu() {
|
81
|
// Rename Save command.
|
82
|
_fileMenuItems[3].setText("Apply");
|
83
|
_fileMenuItems[3].setMnemonic(KeyEvent.VK_A);
|
84
|
|
85
|
// Remove various menu item.
|
86
|
_fileMenu.remove(7);
|
87
|
|
88
|
// _fileMenu.remove(6);
|
89
|
_fileMenu.remove(5);
|
90
|
_fileMenu.remove(4);
|
91
|
_fileMenu.remove(2);
|
92
|
_fileMenu.remove(1);
|
93
|
_fileMenu.remove(0);
|
94
|
}
|
95
|
|
96
|
///////////////////////////////////////////////////////////////////
|
97
|
//// protected methods ////
|
98
|
|
99
|
/** Override to query whether to apply the changes, if any.
|
100
|
* @return False if the user cancels on a apply query.
|
101
|
*/
|
102
|
protected boolean _close() {
|
103
|
// NOTE: The superclass doesn't do the right thing here,
|
104
|
// since it requires an associated Tableau.
|
105
|
// NOTE: We use dispose() here rather than just hiding the
|
106
|
// window. This ensures that derived classes can react to
|
107
|
// windowClosed events rather than overriding the
|
108
|
// windowClosing behavior given here.
|
109
|
boolean returnValue = true;
|
110
|
|
111
|
if (isModified()) {
|
112
|
if (_queryForApply()) {
|
113
|
dispose();
|
114
|
setModified(false); // need this even if discarded
|
115
|
} else {
|
116
|
return false; // cancel
|
117
|
}
|
118
|
} else {
|
119
|
// Window is not modified, so just dispose.
|
120
|
dispose();
|
121
|
}
|
122
|
|
123
|
// If this editor changed the string at all at any time during
|
124
|
// its existence we want the workflow to be marked as modified
|
125
|
if (_wasModified) {
|
126
|
setModified(true);
|
127
|
}
|
128
|
|
129
|
// Ensure that a new editor is opened next time.
|
130
|
this._factory.clear();
|
131
|
|
132
|
return returnValue;
|
133
|
}
|
134
|
|
135
|
/** Override the base class to apply the change to the attribute.
|
136
|
* @return True if the save succeeded.
|
137
|
*/
|
138
|
protected boolean _save() {
|
139
|
// Issue a change request to ensure the change is
|
140
|
// applied at a safe time and that the model is marked
|
141
|
// modified.
|
142
|
NamedObj context = _attributeToEdit.getContainer();
|
143
|
|
144
|
// only issue the change request if the text has actually been changed
|
145
|
if (!_currentText.equals(_factory.getText())) {
|
146
|
String request = "<property name=\"" + _attributeToEdit.getName()
|
147
|
+ "\" value=\""
|
148
|
+ StringUtilities.escapeForXML(_factory.getText()) + "\"/>";
|
149
|
context.requestChange(new MoMLChangeRequest(this, context, request));
|
150
|
_wasModified = true;
|
151
|
_currentText = _factory.getText();
|
152
|
}
|
153
|
setModified(false);
|
154
|
return true;
|
155
|
}
|
156
|
|
157
|
///////////////////////////////////////////////////////////////////
|
158
|
//// private methods ////
|
159
|
// Open a dialog to prompt the user to apply the data.
|
160
|
// Return false if the user clicks "cancel", and otherwise return true.
|
161
|
private boolean _queryForApply() {
|
162
|
Object[] options = { "Apply", "Discard changes", "Cancel" };
|
163
|
String query = "Apply changes to " + _attributeToEdit.getFullName()
|
164
|
+ "?";
|
165
|
|
166
|
// Show the MODAL dialog
|
167
|
int selected = JOptionPane.showOptionDialog(this, query,
|
168
|
"Apply Changes?", JOptionPane.YES_NO_CANCEL_OPTION,
|
169
|
JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
|
170
|
|
171
|
if (selected == 0) {
|
172
|
return _save();
|
173
|
} else if (selected == 1) {
|
174
|
return true;
|
175
|
}
|
176
|
|
177
|
return false;
|
178
|
}
|
179
|
|
180
|
///////////////////////////////////////////////////////////////////
|
181
|
//// private members ////
|
182
|
private final TextEditorFactory _factory;
|
183
|
|
184
|
// Used on editor close to check if the original text was ever modified
|
185
|
private boolean _wasModified;
|
186
|
private String _originalText;
|
187
|
private String _currentText;
|
188
|
|
189
|
private StringAttribute _attributeToEdit;
|
190
|
}
|