001: //Copyright (c) Hans-Joachim Daniels 2005
002: //
003: //This program is free software; you can redistribute it and/or modify
004: //it under the terms of the GNU General Public License as published by
005: //the Free Software Foundation; either version 2 of the License, or
006: //(at your option) any later version.
007: //
008: //This program is distributed in the hope that it will be useful,
009: //but WITHOUT ANY WARRANTY; without even the implied warranty of
010: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: //GNU General Public License for more details.
012: //
013: //You can either finde the file LICENSE or LICENSE.TXT in the source
014: //distribution or in the .jar file of this application
015:
016: package de.uka.ilkd.key.ocl.gf;
017:
018: import java.util.HashSet;
019:
020: /**
021: * @author daniels
022: *
023: * This class represents a fake command, i.e. nothing is send to GF here.
024: * Instead this class acts more like a placeholder for the input dialog.
025: * This dialog is handled in GFEditor2 when a InputCommand is executed.
026: * Reason: No GUI stuff in the command.
027: */
028: class InputCommand extends GFCommand {
029: public static InputCommand intInputCommand = new InputCommand(
030: "read in Integer",
031: "opens a dialog window in which an Integer can be entered",
032: int.class, "Please enter an Integer");
033: public static InputCommand stringInputCommand = new InputCommand(
034: "read in String",
035: "opens a dialog window in which a String can be entered",
036: String.class, "Please enter a String");
037:
038: protected InputCommand(final String description, final String ttt,
039: Class type, final String title) {
040: this .type = type;
041: this .tooltipText = ttt;
042: this .displayText = description;
043: this .titleText = title;
044: this .command = type.getName();
045: }
046:
047: protected Class type;
048:
049: /**
050: * the text that is to be displayed as the title in the input window
051: */
052: protected final String titleText;
053:
054: /**
055: * the text that is to be displayed as the title in the input window
056: */
057: public String getTitleText() {
058: return titleText;
059: }
060:
061: /**
062: * stores the entered values, so they can be offered to the user
063: * the next time, in case, he wants them again.
064: */
065: protected final HashSet enteredValues = new HashSet();
066:
067: /**
068: * the text that is to be displayed as the tooltip
069: */
070: protected final String tooltipText;
071:
072: /**
073: * the text that is to be displayed as the tooltip
074: */
075: public String getTooltipText() {
076: return tooltipText;
077: }
078:
079: /**
080: * the text that is to be displayed in the refinement lists
081: */
082: protected final String displayText;
083:
084: /**
085: * the text that is to be displayed in the refinement lists
086: */
087: public String getDisplayText() {
088: return displayText;
089: }
090:
091: /**
092: * the subcategory of this command
093: */
094: public String getSubcat() {
095: return null;
096: }
097:
098: /**
099: * Checks if the given String can be converted into
100: * the Type of this InputCommand (int or String).
101: * If that is possible, the converted object is saved
102: * in enteredValues for later redisplay for the user.
103: * @param o The String the user has typed
104: * @param reason If the entered String is not parseable as the expected
105: * type, an error message is appended to this StringBuffer, so better
106: * give an empty one.
107: * @return an Object whose toString() should send the right
108: * thing to GF.
109: * Maybe null, if this "conversion" failed.
110: */
111: protected Object validate(String o, StringBuffer reason) {
112: Object result = null;
113: if (type == int.class) {
114: int i;
115: try {
116: i = Integer.parseInt(o);
117: result = new Integer(i);
118: } catch (NumberFormatException e) {
119: reason.append("Input format error: '" + o
120: + "' is no Integer");
121: }
122: } else if (type == String.class) {
123: if (o != null) {
124: result = "\"" + o.toString() + "\"";
125: }
126: }
127: if (result != null) {
128: this .enteredValues.add(result);
129: }
130: return result;
131: }
132:
133: /**
134: * selects the suiting InputCommand for the given full name of a type
135: * @param typeName at the moment, either int.class.getName() or String.class.getName()
136: * @return intInputCommand for int, stringInputCommand for String or null otherwise
137: */
138: protected static InputCommand forTypeName(String typeName) {
139: InputCommand ic = null;
140: if (typeName.equals(int.class.getName())) {
141: ic = InputCommand.intInputCommand;
142: } else if (typeName.equals(String.class.getName())) {
143: ic = InputCommand.stringInputCommand;
144: }
145: return ic;
146: }
147:
148: }
|