1
/* -*- tab-width: 4 -*-
3
* Electric(tm) VLSI Design System
5
* File: TextAttributesPanel.java
7
* Copyright (c) 2003 Sun Microsystems and Static Free Software
9
* Electric(tm) is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 3 of the License, or
12
* (at your option) any later version.
14
* Electric(tm) is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with Electric(tm); see the file COPYING. If not, write to
21
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22
* Boston, Mass 02111-1307, USA.
25
package com.sun.electric.tool.user.dialogs;
27
import com.sun.electric.database.hierarchy.Cell;
28
import com.sun.electric.database.topology.NodeInst;
29
import com.sun.electric.database.variable.CodeExpression;
30
import com.sun.electric.database.variable.ElectricObject;
31
import com.sun.electric.database.variable.TextDescriptor;
32
import com.sun.electric.database.variable.Variable;
33
import com.sun.electric.tool.Job;
34
import com.sun.electric.tool.JobException;
35
import com.sun.electric.tool.user.User;
37
import java.util.Iterator;
39
import javax.swing.JPanel;
42
* A Panel to display Code, Units, ShowStyle, and isParameter/isInherits
43
* information about a Variable (or TextDescriptor, if passed Variable is null).
45
public class TextAttributesPanel extends JPanel
47
private static final String displaynone = "None";
49
private boolean updateChangesInstantly;
50
private boolean loading = false;
52
private TextDescriptor td;
53
private Variable.Key varKey;
54
private ElectricObject owner;
55
private TextDescriptor.Unit initialUnit;
56
private Object initialDispPos; // this needs to be an object because one choice, "none" is a string
57
// instead of a TextDescriptor.DispPos
58
private CodeExpression.Code initialCode;
61
* Create a Panel for editing attribute specific
62
* text options of a Variable
64
public TextAttributesPanel(boolean updateChangesInstantly)
66
this.updateChangesInstantly = updateChangesInstantly;
69
// add variable code types
70
for (Iterator<CodeExpression.Code> it = CodeExpression.Code.getCodes(); it.hasNext(); ) {
71
code.addItem(it.next());
74
// populate units dialog box
75
for (Iterator<TextDescriptor.Unit> it = TextDescriptor.Unit.getUnits(); it.hasNext(); ) {
76
units.addItem(it.next());
79
// populate show style dialog box
80
populateShowComboBox(true);
85
initialCode = CodeExpression.Code.NONE;
86
code.setSelectedItem(CodeExpression.Code.NONE);
88
initialUnit = TextDescriptor.Unit.NONE;
89
units.setSelectedItem(initialUnit);
91
initialDispPos = TextDescriptor.DispPos.NAMEVALUE;
92
show.setSelectedItem(initialDispPos);
94
// dialog is disabled by default
95
setVariable(null, null);
98
code.addActionListener(new java.awt.event.ActionListener()
100
public void actionPerformed(java.awt.event.ActionEvent evt) { fieldChanged(); }
102
units.addActionListener(new java.awt.event.ActionListener()
104
public void actionPerformed(java.awt.event.ActionEvent evt) { fieldChanged(); }
106
show.addActionListener(new java.awt.event.ActionListener()
108
public void actionPerformed(java.awt.event.ActionEvent evt) { fieldChanged(); }
112
private void fieldChanged()
114
if (!updateChangesInstantly) return;
120
* Set the Variable that can be edited through this Panel.
121
* <p>if owner.getTextDescriptor(varKey) returns non-null td, display and allow editing of the td text options
122
* <p>else if varKey is non-null, display and allow editing of default values.
123
* <p>if varKey is null, the entire Panel is disabled.
124
* @param varKey the key of a variable to be changed
125
* @param owner the owner of the variable
127
public synchronized void setVariable(Variable.Key varKey, ElectricObject owner) {
131
// default information
134
attrInfo1.setText("YOU ARE RUNNING IN DEBUG MODE, SO:");
135
attrInfo2.setText("To create a parameter on a cell");
136
attrInfo3.setText("its name must start with 'ATTR_'");
139
attrInfo1.setText("");
140
attrInfo2.setText("");
141
attrInfo3.setText("");
144
// do not allow empty var names
145
if (varKey != null) {
146
if (varKey.getName().trim().equals("")) varKey = null;
149
this.varKey = varKey;
152
boolean enabled = owner != null && varKey != null;
154
// update enabled state of everything
155
// can't just enable all children because objects might be inside JPanel
156
code.setEnabled(enabled);
157
units.setEnabled(enabled);
158
show.setEnabled(enabled);
160
if (!enabled) return;
162
// if td is null (implies var is null)
163
// then use the current panel values to apply to varName.
164
td = owner.getTextDescriptor(varKey);
165
if (td == null) return;
166
var = owner.getParameterOrVariable(varKey);
171
initialCode = CodeExpression.Code.NONE;
173
initialCode = var.getCode();
174
code.setSelectedItem(initialCode);
176
// var null, disable code
177
code.setEnabled(false);
180
initialUnit = td.getUnit();
181
units.setSelectedItem(td.getUnit());
184
initialDispPos = td.getDispPart();
186
initialDispPos = displaynone;
187
// show style is none if var non-null and isDisplay is false
189
// make sure "none" is a choice
190
populateShowComboBox(true);
191
if (!var.isDisplay()) {
192
show.setSelectedIndex(0);
194
show.setSelectedItem(initialDispPos);
196
show.setEnabled(false);
200
populateShowComboBox(false);
201
show.setSelectedItem(initialDispPos);
204
if (owner instanceof Cell && !td.isInherit())
206
attrInfo1.setText("NOTE: This cell attribute is NOT");
207
attrInfo2.setText("inherited by instances of the cell");
214
* Method to modify a TextDescriptor to match the settings in this panel.
215
* @param td the input TextDescriptor.
216
* @return the TextDescriptor with code/units/display in this panel.
218
public TextDescriptor withPanelValues(TextDescriptor td)
221
TextDescriptor.Unit newUnit = (TextDescriptor.Unit)units.getSelectedItem();
222
td = td.withUnit(newUnit);
224
// change the show style
225
Object newDispObj = show.getSelectedItem();
226
if (newDispObj == displaynone)
228
td = td.withDisplay(false);
231
td = td.withDisplay(true);
232
TextDescriptor.DispPos newDisp = (TextDescriptor.DispPos)newDispObj;
233
td = td.withDispPart(newDisp);
239
* Method to modify a variable value to match the Code settings in this panel.
240
* @param value the input value
241
* @return the value with code in this panel.
243
public Object withPanelCode(Object value)
245
// change the code type
246
CodeExpression.Code newCode = (CodeExpression.Code)code.getSelectedItem();
247
return Variable.withCode(value, newCode);
251
* Apply any changes the user has made through the Panel.
252
* @return true if any changes committed to database, false otherwise
254
public synchronized boolean applyChanges() {
255
if (varKey == null) return false;
257
boolean changed = false;
259
// see if code changed
260
CodeExpression.Code newCode = (CodeExpression.Code)code.getSelectedItem();
261
if (newCode != initialCode) changed = true;
262
// see if units changed
263
TextDescriptor.Unit newUnit = (TextDescriptor.Unit)units.getSelectedItem();
264
if (newUnit != initialUnit) changed = true;
265
// see if show style changed - check if DispPos changed
266
Object newDisp = show.getSelectedItem();
267
if (newDisp != initialDispPos) changed = true;
268
int newDispIndex = -1;
269
if (newDisp != displaynone)
270
newDispIndex = ((TextDescriptor.DispPos)newDisp).getIndex();
273
// nothing changed on current var/td, return
274
if (!changed) return false;
285
initialCode = newCode;
286
initialUnit = newUnit;
287
initialDispPos = newDisp;
291
// populate show combo box. If includeNoneChoice is true, include
292
// the "None" option in the combo box. Note that it is a String, while
293
// all the other objects are TextDescriptor.DispPos objects.
294
private void populateShowComboBox(boolean includeNoneChoice) {
295
show.removeAllItems();
296
// populate show style dialog box
297
if (includeNoneChoice) show.addItem(displaynone);
298
for (Iterator<TextDescriptor.DispPos> it = TextDescriptor.DispPos.getShowStyles(); it.hasNext(); ) {
299
show.addItem(it.next());
304
private static class ChangeText extends Job {
306
private ElectricObject owner;
307
private Variable.Key varKey;
308
private CodeExpression.Code code;
313
ElectricObject owner,
315
CodeExpression.Code code,
319
super("Modify Text Attribute", User.getUserTool(), Job.Type.CHANGE, null, null, Job.Priority.USER);
321
this.varKey = varKey;
324
this.dispPos = dispPos;
328
public boolean doIt() throws JobException {
329
// change the code type
330
if (owner.isParam(varKey)) {
331
if (owner instanceof Cell) {
332
Cell cell = (Cell)owner;
333
cell.getCellGroup().updateParam((Variable.AttrKey)varKey, cell.getParameter(varKey).withCode(code).getObject());
334
} else if (owner instanceof NodeInst) {
335
NodeInst ni = (NodeInst)owner;
336
ni.addParameter(ni.getParameter(varKey).withCode(code));
339
owner.updateVarCode(varKey, code);
342
TextDescriptor td = owner.getTextDescriptor(varKey);
343
if (td == null) return false;
346
td = td.withUnit(TextDescriptor.Unit.getUnitAt(unit));
347
// change the show style
348
Variable var = owner.getParameterOrVariable(varKey);
350
// var should not be null
351
if (var != null) td = td.withDisplay(false);
353
if (var != null) td = td.withDisplay(true);
354
td = td.withDispPart(TextDescriptor.DispPos.getShowStylesAt(dispPos));
356
owner.setTextDescriptor(varKey, td);
361
/** This method is called from within the constructor to
362
* initialize the form.
363
* WARNING: Do NOT modify this code. The content of this method is
364
* always regenerated by the Form Editor.
366
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
367
private void initComponents() {
368
java.awt.GridBagConstraints gridBagConstraints;
370
jPanel1 = new javax.swing.JPanel();
371
jLabel1 = new javax.swing.JLabel();
372
code = new javax.swing.JComboBox();
373
jLabel2 = new javax.swing.JLabel();
374
units = new javax.swing.JComboBox();
375
jLabel3 = new javax.swing.JLabel();
376
show = new javax.swing.JComboBox();
377
jPanel2 = new javax.swing.JPanel();
378
attrInfo1 = new javax.swing.JLabel();
379
attrInfo2 = new javax.swing.JLabel();
380
attrInfo3 = new javax.swing.JLabel();
382
setLayout(new java.awt.GridBagLayout());
384
setBorder(javax.swing.BorderFactory.createEtchedBorder());
385
jPanel1.setLayout(new java.awt.GridBagLayout());
387
jLabel1.setText("Code:");
388
gridBagConstraints = new java.awt.GridBagConstraints();
389
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
390
gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
391
jPanel1.add(jLabel1, gridBagConstraints);
393
gridBagConstraints = new java.awt.GridBagConstraints();
394
gridBagConstraints.gridx = 1;
395
gridBagConstraints.gridy = 0;
396
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
397
gridBagConstraints.weightx = 0.1;
398
jPanel1.add(code, gridBagConstraints);
400
jLabel2.setText("Units:");
401
gridBagConstraints = new java.awt.GridBagConstraints();
402
gridBagConstraints.gridx = 0;
403
gridBagConstraints.gridy = 1;
404
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
405
gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
406
jPanel1.add(jLabel2, gridBagConstraints);
408
gridBagConstraints = new java.awt.GridBagConstraints();
409
gridBagConstraints.gridx = 1;
410
gridBagConstraints.gridy = 1;
411
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
412
gridBagConstraints.weightx = 0.1;
413
jPanel1.add(units, gridBagConstraints);
415
jLabel3.setText("Show:");
416
gridBagConstraints = new java.awt.GridBagConstraints();
417
gridBagConstraints.gridx = 0;
418
gridBagConstraints.gridy = 2;
419
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
420
gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
421
jPanel1.add(jLabel3, gridBagConstraints);
423
gridBagConstraints = new java.awt.GridBagConstraints();
424
gridBagConstraints.gridx = 1;
425
gridBagConstraints.gridy = 2;
426
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
427
gridBagConstraints.weightx = 0.1;
428
jPanel1.add(show, gridBagConstraints);
430
gridBagConstraints = new java.awt.GridBagConstraints();
431
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
432
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
433
gridBagConstraints.weightx = 1.0;
434
gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
435
add(jPanel1, gridBagConstraints);
437
jPanel2.setLayout(new java.awt.GridBagLayout());
439
attrInfo1.setText("Attributes created on a cell are");
440
gridBagConstraints = new java.awt.GridBagConstraints();
441
gridBagConstraints.gridx = 0;
442
gridBagConstraints.gridy = 0;
443
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
444
jPanel2.add(attrInfo1, gridBagConstraints);
446
attrInfo2.setText("inherited by instances of that cell");
447
gridBagConstraints = new java.awt.GridBagConstraints();
448
gridBagConstraints.gridx = 0;
449
gridBagConstraints.gridy = 1;
450
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
451
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
452
jPanel2.add(attrInfo2, gridBagConstraints);
454
gridBagConstraints = new java.awt.GridBagConstraints();
455
gridBagConstraints.gridx = 0;
456
gridBagConstraints.gridy = 2;
457
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
458
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
459
jPanel2.add(attrInfo3, gridBagConstraints);
461
gridBagConstraints = new java.awt.GridBagConstraints();
462
gridBagConstraints.gridx = 1;
463
gridBagConstraints.gridy = 0;
464
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
465
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
466
gridBagConstraints.insets = new java.awt.Insets(4, 10, 4, 4);
467
add(jPanel2, gridBagConstraints);
469
}// </editor-fold>//GEN-END:initComponents
471
// Variables declaration - do not modify//GEN-BEGIN:variables
472
private javax.swing.JLabel attrInfo1;
473
private javax.swing.JLabel attrInfo2;
474
private javax.swing.JLabel attrInfo3;
475
private javax.swing.JComboBox code;
476
private javax.swing.JLabel jLabel1;
477
private javax.swing.JLabel jLabel2;
478
private javax.swing.JLabel jLabel3;
479
private javax.swing.JPanel jPanel1;
480
private javax.swing.JPanel jPanel2;
481
private javax.swing.JComboBox show;
482
private javax.swing.JComboBox units;
483
// End of variables declaration//GEN-END:variables