/* An Object for changing the style of parameters. Copyright (c) 1998-2014 The Regents of the University of California. All rights reserved. Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, and distribute this software and its documentation for any purpose, provided that the above copyright notice and the following two paragraphs appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. PT_COPYRIGHT_VERSION_2 COPYRIGHTENDKEY */ package ptolemy.actor.gui.style; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.swing.BoxLayout; import ptolemy.actor.gui.Configurer; import ptolemy.data.expr.Parameter; import ptolemy.gui.Query; import ptolemy.gui.QueryListener; import ptolemy.gui.Top; import ptolemy.kernel.util.Attribute; import ptolemy.kernel.util.IllegalActionException; import ptolemy.kernel.util.InternalErrorException; import ptolemy.kernel.util.KernelException; import ptolemy.kernel.util.NameDuplicationException; import ptolemy.kernel.util.NamedObj; import ptolemy.kernel.util.Settable; import ptolemy.moml.MoMLChangeRequest; /////////////////////////////////////////////////////////////////// //// StyleConfigurer /** This class is an editor for the styles of the parameters of an object. It allows a user to graphically change the ParameterEditorStyles contained within the user settable attributes of a named object. It is very similar in spirit and style to Configurer, which edits the actual values of the attributes.
The restore() method restores the values of the parameters of the
object to their values when this object was created. This can be used
in a modal dialog to implement a cancel button, which restores
the styles to those before the dialog was opened.
@see ptolemy.actor.gui.Configurer
@see ParameterEditorStyle
@author Steve Neuendorffer and Edward A. Lee
@version $Id: StyleConfigurer.java 70402 2014-10-23 00:52:20Z cxh $
@since Ptolemy II 1.0
@Pt.ProposedRating Yellow (neuendor)
@Pt.AcceptedRating Yellow (neuendor)
*/
@SuppressWarnings("serial")
public class StyleConfigurer extends Query implements QueryListener {
/** Construct a configurer for the specified object.
* @param object The object to configure.
* @exception IllegalActionException If the specified object has
* no editor factories, and refuses to acceptable as an attribute
* an instance of EditorPaneFactory.
*/
public StyleConfigurer(NamedObj object) throws IllegalActionException {
super();
this.addQueryListener(this);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
_object = object;
setTextWidth(25);
try {
// FIXME this list should not be statically specified.
// Note that fixing this will probably move the accept method
// into some sort of factory object (instead of cloning
// existing styles).
// NOTE: These styles need to have a container so
// that exportMoML() doesn't generate XML header information.
Parameter container = new Parameter();
_parameterStyles = new ParameterEditorStyle[8];
_parameterStyles[0] = new LineStyle(container, "Line");
_parameterStyles[1] = new CheckBoxStyle(container, "Check Box");
_parameterStyles[2] = new ChoiceStyle(container, "Choice");
_parameterStyles[3] = new EditableChoiceStyle(container,
"EditableChoice");
_parameterStyles[4] = new TextStyle(container, "Text");
_parameterStyles[5] = new FileChooserStyle(container, "FileChooser");
_parameterStyles[6] = new NotEditableLineStyle(container, "Fixed");
_parameterStyles[7] = new HiddenStyle(container, "Hidden");
} catch (NameDuplicationException ex) {
throw new InternalErrorException(ex.getMessage());
}
Iterator parameters = object.attributeList(Settable.class).iterator();
while (parameters.hasNext()) {
Settable param = (Settable) parameters.next();
// Skip if the parameter is not visible.
if (!Configurer.isVisible(_object, param)) {
continue;
}
// Get the current style.
boolean foundOne = false;
Iterator styles = ((NamedObj) param).attributeList(
ParameterEditorStyle.class).iterator();
ParameterEditorStyle foundStyle = null;
while (styles.hasNext()) {
foundOne = true;
foundStyle = (ParameterEditorStyle) styles.next();
}
List styleList = new ArrayList();
// The index of the default;
int defaultIndex = 0;
_originalExpertMode = _object.getAttribute("_expertMode") != null;
if (param.getVisibility() == Settable.NOT_EDITABLE
&& !_originalExpertMode && !foundOne) {
// If the parameter is set to NOT_EDITABLE visibility and not expert mode
// then only a fixed style is possible.
styleList.add("Fixed");
defaultIndex = 0;
} else {
int count = 0;
// Reduce the list of parameters
for (int i = 0; i < _parameterStyles.length
&& _parameterStyles[i] != null; i++) {
if (foundOne
&& _parameterStyles[i].getClass() == foundStyle
.getClass()) {
defaultIndex = count;
if (foundStyle.acceptable(param)) {
styleList.add(_parameterStyles[i].getName());
count++;
}
} else if (_parameterStyles[i].acceptable(param)) {
styleList.add(_parameterStyles[i].getName());
count++;
}
}
}
String[] styleArray = (String[]) styleList
.toArray(new String[styleList.size()]);
addChoice(param.getName(), param.getName(), styleArray,
styleArray[defaultIndex]);
}
// Add the expert mode box.
addCheckBox("expertMode", "expert mode", _originalExpertMode);
}
///////////////////////////////////////////////////////////////////
//// public methods ////
/** Generate a change request to apply the changes.
* This is called to notify that one of the entries has changed.
* The name of the entry is passed as an argument.
* @param name The name of the entry.
*/
@Override
public void changed(String name) {
StringBuffer moml = new StringBuffer();
// Treat the expertMode entry specially.
if (name.equals("expertMode")) {
Attribute previousExpert = _object.getAttribute("_expertMode");
boolean isExpert = previousExpert != null;
boolean toExpert = getBooleanValue("expertMode");
if (isExpert != toExpert) {
if (isExpert) {
moml.append("