Changeset 14784
- Timestamp:
- 2007-11-07T11:13:16+13:00 (16 years ago)
- Location:
- gli/branches/2.75/src/org/greenstone/gatherer/cdm
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
gli/branches/2.75/src/org/greenstone/gatherer/cdm/Argument.java
r12824 r14784 51 51 */ 52 52 public class Argument 53 implements Comparable, Serializable { 54 /** An element of the argument type enumeration specifying a combobox control. */ 55 static final public byte ENUM = 0; 56 /** An element of the argument type enumeration specifying a checkbox control. */ 57 static final public byte FLAG = 1; 58 /** An element of the argument type enumeration specifying a tree control. */ 59 static final public byte HIERARCHY = 2; 60 /** An element of the argument type enumeration specifying a spinner control. */ 61 static final public byte INTEGER = 3; 62 /** An element of the argument type enumeration specifying a language combobox control. */ 63 static final public byte LANGUAGE = 4; 64 /** An element of the argument type enumeration specifying a list control. */ 65 static final public byte METADATA = 5; 66 /** An element of the argument type enumeration specifying a metadata combobox control. */ 67 static final public byte METADATUM = 6; 68 /** An element of the argument type enumeration specifying a text field. */ 69 static final public byte STRING = 7; 70 /** An element of the argument type enumeration specifying a regular expression text field. */ 71 static final public byte REGEXP = 8; 72 /** An element of the argument type enumeration specifying a metadata set combobox control. */ 73 static final public byte METADATA_SET_NAMESPACE = 9; 74 75 ///////////kk added the number was 9, I changed it to 10////////////// 76 /** An element of the argument type enumeration specifying a text field. */ 77 static final public byte URL = 10; 78 ///////////////////////////////////////////////////////////////// 79 80 /** true if this argument should actually be hidden within the GLI. This is important for arguments such as import dir or other location critical arguments. */ 81 private boolean hidden_gli = false; 82 /** <i>true</i> if this argument is required for the applicable script to work properly, <i>false</i> otherwise. */ 83 private boolean required = false; 84 /** The type of this argument. Used to be an int, but bytes are cheaper. */ 85 private byte type = STRING; 86 /** The maximum value an integer based control can have. */ 87 private int maximum = Integer.MAX_VALUE; 88 /** The minimum value an integer based control can have. */ 89 private int minimum = Integer.MIN_VALUE; 90 /** Every argument has a detail mode level at which it becomes available to the user to edit. 91 * @see org.greenstone.gatherer.Configuration 92 */ 93 private int mode_level = Configuration.LIBRARIAN_MODE; 94 /** The DOM element this argument is built around, if any. */ 95 private Element element; 96 /** If the argument is of type ENUM then this map holds all the various options. Each entry is an <option value> -> <description> mapping. */ 97 private ArrayList option_list = null; 98 /** A default value for parameter-type arguments. May be a Perl pattern. */ 99 private String default_value = null; 100 /** The text description of this argument parsed from the pluginfo output. */ 101 private String description = null; 102 /** The argument flag as it appears in the command. Also used as the unique identifier of an argument. */ 103 private String name = null; 104 /** The plugin that owns this argument, for the purposes of visualising inheritance. */ 105 private String owner = null; 106 107 private String display_name = null; 108 109 /** Default Constructor. */ 110 public Argument() { 111 } 112 113 /** Another constructor but this one is a little more interesting as it takes a DOM element. 114 * @param element the Element this argument is based around 115 */ 116 public Argument(Element element) { 117 this.element = element; 118 } 119 120 /** Method to add an element to the option_list. 121 * @param name the name value of the option as a String 122 * @param desc the description of this options as a String 123 */ 124 public void addOption(String name, String desc) { 125 if(type == ENUM && name != null) { 126 if(desc == null) { 127 desc = ""; 128 } 129 if(option_list == null) { 130 option_list = new ArrayList(); 131 } 132 option_list.add(new ArgumentOption(name, desc)); 133 } 134 } 135 136 /** Method to compare two arguments for ordering. 137 * @param object the argument we are comparing to, as an Object 138 * @return an int specifying the argument order, using values as set out in String 139 * @see org.greenstone.gatherer.cdm.Argument 140 */ 141 public int compareTo(Object object) { 142 if(object instanceof Argument) { 143 return getName().compareTo(((Argument)object).getName()); 144 } 145 else { 146 return toString().compareTo(object.toString()); 147 } 148 } 149 150 /** Create a copy of this argument. 151 * @return a newly created Argument with the same details as this one 152 */ 153 public Argument copy() { 154 Argument copy = new Argument(); 155 copy.setDefaultValue(default_value); 156 copy.setDescription(description); 157 copy.setOptions(option_list); 158 copy.setOwner(owner); 159 copy.setName(name); 160 copy.setRequired(required); 161 copy.setType(type); 162 copy.setMinimum(minimum); 163 copy.setMaximum(maximum); 164 copy.setModeLevel(mode_level); 165 copy.setHiddenGLI(hidden_gli); 166 return copy; 167 } 168 169 /** Method to determine if two arguments are equal. 170 * @param object the argument to test against, as an Object 171 * @return true if the arguments names match, false otherwise 172 */ 173 public boolean equals(Object object) { 174 return (compareTo(object) == 0); 175 } 176 177 /** Method to retrieve the value of default_value. 178 * @return a String containing the default value 179 */ 180 public String getDefaultValue() { 181 return default_value; 182 } 183 184 /** Method to retrieve this arguments description. 185 * @return a String containing the description 186 */ 187 public String getDescription() { 188 return description; 189 } 190 191 public Element getElement() { 192 return element; 193 } 194 /** Retrieve the upper bound of a range based argument. 195 * @return the maximum as an int 196 */ 197 public int getMaximum() { 198 return maximum; 199 } 200 201 /** Retrieve the lower bound of a range based argument. 202 * @return the minimum as an int 203 */ 204 public int getMinimum() { 205 return minimum; 206 } 207 208 /** Retrieves the mode level at which this argument should become available. Any higher levels should also see this argument. 209 * @return the mode level as an int 210 */ 211 public int getModeLevel() { 212 return mode_level; 213 } 214 215 /** Method to retrieve the value of name. 216 * @return a String containing the argument name 217 * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE 218 */ 219 public String getName() { 220 if(name == null && element != null) { 221 name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE); 222 } 223 return name; 224 } 225 226 public String getDisplayName() { 227 return display_name; 228 } 229 230 231 /** Method to retrieve the option list for this argument. 232 * @return a HashMap containing <option value> -> <description> entries 233 */ 234 public ArrayList getOptions() { 235 return option_list; 236 } 237 238 /** Retrieve the name of the owner of this argument. 239 * @return the owners name as a String 240 */ 241 public String getOwner() { 242 return owner; 243 } 244 245 /** Method to determine the type of this argument. 246 * @return a byte specifying the type 247 */ 248 public byte getType() { 249 return type; 250 } 251 252 /** Method to retrieve the value of value. 253 * @return the value of value as a String 254 * @see org.greenstone.gatherer.Gatherer#c_man 255 * @see org.greenstone.gatherer.collection.CollectionManager#getCollection 256 */ 257 public String getValue() 258 { 259 // Only assigned arguments have values. 260 if (element == null) { 261 return null; 262 } 263 264 return XMLTools.getValue(element); 265 } 266 267 268 /** Method to determine if this argument has been assigned. 269 * @return true if it has, false otherwise 270 * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE 271 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR 272 */ 273 public boolean isAssigned() { 274 return (element != null && element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.TRUE_STR)); 275 } 276 277 /** Determine if this is a custom argument ie one that has been parsed from the config file but doesn't have a matching entry in the argument library. 278 * @return true if this argument is a custom, false otherwise 279 * @see org.greenstone.gatherer.util.StaticStrings#CUSTOM_ATTRIBUTE 280 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR 281 */ 282 public boolean isCustomArgument() { 283 return (element != null && element.getAttribute(StaticStrings.CUSTOM_ATTRIBUTE).equals(StaticStrings.TRUE_STR)); 284 } 285 286 /** Determine if this argument is hidden in GLI 287 * @return true if the argument is hidden, false otherwise 288 */ 289 public boolean isHiddenGLI() { 290 return hidden_gli; 291 } 292 293 /** Method to determine of this argument is required for the associated script to work. 294 * @return true if this argument is required, false otherwise 295 */ 296 public boolean isRequired() { 297 return required; 298 } 299 300 /** Method to allow for the activation of arguments that might never have their setValue() method called. 301 * @param assigned the desired state as a boolean 302 * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE 303 * @see org.greenstone.gatherer.util.StaticStrings#FALSE_STR 304 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR 305 */ 306 public void setAssigned(boolean assigned) { 307 if(element != null) { 308 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR)); 309 } 310 } 311 312 /** Sets the value of default_value. 313 * @param default_value The new value for default_value as a <strong>String</strong>. 314 */ 315 public void setDefaultValue(String default_value) { 316 this.default_value = default_value; 317 } 318 319 /** Set the value of desc. 320 * @param description the new value of desc as a String 321 */ 322 public void setDescription(String description) { 323 this.description = description; 324 } 325 326 /** Set the element this argument should be based upon. 327 * @param element the Element 328 */ 329 public void setElement(Element element) { 330 this.element = element; 331 } 332 333 /** Mark this argument as being hidden in GLI. */ 334 public void setHiddenGLI(boolean hidden) { 335 this.hidden_gli = hidden; 336 } 337 338 /** Set the upper bound for a range type argument. 339 * @param maximum the maximum as an int 340 */ 341 public void setMaximum(int maximum) { 342 this.maximum = maximum; 343 } 344 345 /** Set the lower bound for a range type argument. 346 * @param minimum the minimum as an int 347 */ 348 public void setMinimum(int minimum) { 349 this.minimum = minimum; 350 } 351 352 /** Set the detail mode level where this argument will become available. 353 * @param mode_level the mode level as an int 354 */ 355 public void setModeLevel(int mode_level) { 356 this.mode_level = mode_level; 357 } 358 359 /** Set the value of name. 360 * @param name the new value of name as a String 361 */ 362 public void setName(String name) { 363 this.name = name; 364 } 365 366 /** Sets the value of the options list. 367 * @param list the new options list as a HashMap 368 */ 369 public void setOptions(ArrayList list) { 370 this.option_list = list; 371 } 372 373 /** Set the owner of this argument. 374 * @param owner the name of the owner of this argument as a String 375 */ 376 public void setOwner(String owner) { 377 this.owner = owner; 378 } 379 380 /** Set the value of required. 381 * @param required the new value of required as a boolean 382 */ 383 public void setRequired(boolean required) { 384 this.required = required; 385 } 386 387 /** Set the value of type. 388 * @param type the new value of type as an byte 389 */ 390 public void setType(byte type) { 391 this.type = type; 392 } 393 394 /** Set the value of type, by matching a type to the given string. 395 * @param new_type a String which contains the name of a certain argument type 396 * @see org.greenstone.gatherer.util.StaticStrings#ENUM_STR 397 * @see org.greenstone.gatherer.util.StaticStrings#FLAG_STR 398 * @see org.greenstone.gatherer.util.StaticStrings#HIERARCHY_STR 399 * @see org.greenstone.gatherer.util.StaticStrings#INT_STR 400 * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGE_STR 401 * @see org.greenstone.gatherer.util.StaticStrings#METADATA_TYPE_STR 402 * @see org.greenstone.gatherer.util.StaticStrings#METADATUM_TYPE_STR 403 * @see org.greenstone.gatherer.util.StaticStrings#REGEXP_STR 404 */ 405 public void setType(String new_type) { 406 if(new_type.equalsIgnoreCase(StaticStrings.ENUM_STR)) { 407 this.type = ENUM; 408 option_list = new ArrayList(); 409 } 410 else if(new_type.equalsIgnoreCase(StaticStrings.FLAG_STR)) { 411 this.type = FLAG; 412 } 413 else if(new_type.equalsIgnoreCase(StaticStrings.HIERARCHY_STR)) { 414 this.type = HIERARCHY; 415 } 416 else if(new_type.equalsIgnoreCase(StaticStrings.INT_STR)) { 417 this.type = INTEGER; 418 } 419 else if(new_type.equalsIgnoreCase(StaticStrings.LANGUAGE_STR)) { 420 this.type = LANGUAGE; 421 } 422 else if(new_type.equalsIgnoreCase(StaticStrings.METADATA_TYPE_STR)) { 423 this.type = METADATA; 424 } 425 else if(new_type.equalsIgnoreCase(StaticStrings.METADATUM_TYPE_STR)) { 426 this.type = METADATUM; 427 } 428 else if(new_type.equalsIgnoreCase(StaticStrings.REGEXP_STR)) { 429 this.type = REGEXP; 430 } 431 else { 432 this.type = STRING; 433 } 434 } 435 436 /** Method to set the value of this argument. 437 * @param value the new value for the argument 438 * @see org.greenstone.gatherer.Gatherer#println 439 */ 440 public void setValue(String value) { 441 if(element != null) { 442 XMLTools.setValue(element, value); 443 } 444 else { 445 DebugStream.println("Argument.setValue(" + value + ") called on a base Argument."); 446 } 447 } 448 449 /** Set the values vector to the given values. Currently I just assign the new values, whereas I may later want to implement a deep clone. 450 * @param values an ArrayList of values 451 * @see org.greenstone.gatherer.Gatherer#println 452 */ 453 public void setValues(ArrayList values) { 454 if(element != null) { 455 StringBuffer value = new StringBuffer(); 456 int value_length = values.size(); 457 for(int i = 0; i < value_length; i++) { 458 value.append(values.get(i)); 459 value.append(StaticStrings.COMMA_CHARACTER); 460 } 461 value.deleteCharAt(value.length() - 1); // Remove last ',' 462 XMLTools.setValue(element, value.toString()); 463 } 464 else { 465 DebugStream.println("Argument.setValues([" + values.size() + " items]) called on a base Argument."); 466 } 467 } 468 469 /** Method for translating the data of this class into a string. 470 * @return a String containing a fragment of the total arguments string 471 * @see org.greenstone.gatherer.Gatherer#c_man 472 * @see org.greenstone.gatherer.collection.CollectionManager#getCollection 473 * @see org.greenstone.gatherer.util.StaticStrings#COMMA_CHARACTER 474 * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE 475 * @see org.greenstone.gatherer.util.StaticStrings#SPACE_CHARACTER 476 * @see org.greenstone.gatherer.util.StaticStrings#SPEECH_CHARACTER 477 */ 478 public String toString() 479 { 480 StringBuffer text = new StringBuffer("-"); 481 482 if (element == null) { 483 return text.toString(); 484 } 485 486 if (name == null) { 487 name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE); 488 } 489 text.append(name); 490 491 String value = XMLTools.getValue(element); 492 if (value.length() == 0) { 493 return text.toString(); 494 } 495 496 text.append(StaticStrings.SPACE_CHARACTER); 497 498 // // Handle metadata elements specially 499 // if (type == METADATA || type == METADATUM) { 500 // // Tokenize the string 501 // StringTokenizer tokenizer = new StringTokenizer(value, ","); 502 // while (tokenizer.hasMoreTokens()) { 503 // String token = tokenizer.nextToken(); 504 505 // MetadataElement metadata_element = MetadataTools.getMetadataElementWithDisplayName(token); 506 // if (metadata_element != null) { 507 // text.append(metadata_element.getFullName()); 508 // } 509 // else { 510 // text.append(token); 511 // } 512 513 // if (tokenizer.hasMoreTokens()) { 514 // text.append(StaticStrings.COMMA_CHARACTER); 515 // } 516 // } 517 // return text.toString(); 518 // } 519 520 // If the value contains a space, add speech marks 521 // (Except for metadata elements, which won't have spaces when written out to collect.cfg) 522 if (value.indexOf(StaticStrings.SPACE_CHARACTER) != -1 && !(type == METADATUM || type == METADATA)) { 523 value = StaticStrings.SPEECH_CHARACTER + value + StaticStrings.SPEECH_CHARACTER; 524 } 525 526 text.append(value); 527 return text.toString(); 528 } 529 530 /** parse the <Option> XML from eg import.pl -xml or pluginfo.pl -xml */ 531 public void parseXML(Element option) { 532 533 for(Node node = option.getFirstChild(); node != null; node = node.getNextSibling()) { 534 String node_name = node.getNodeName(); 535 if(node_name.equals("Name")) { 536 setName(XMLTools.getValue(node)); 537 } 538 else if(node_name.equals("Desc")) { 539 setDescription(XMLTools.getValue(node)); 540 } 541 else if(node_name.equals("Type")) { 542 setType(XMLTools.getValue(node)); 543 } 544 else if(node_name.equals("Default")) { 545 setDefaultValue(XMLTools.getValue(node)); 546 } 547 else if(node_name.equals("Required")) { 548 String v = XMLTools.getValue(node); 549 if(v != null && v.equals("yes")) { 550 setRequired(true); 551 } 552 } 553 else if(node_name.equals("List")) { 554 // Two final loops are required to parse lists. 555 for(Node value = node.getFirstChild(); value != null; value = value.getNextSibling()) { 556 if(value.getNodeName().equals("Value")) { 557 String key = null; 558 String desc = ""; 559 for(Node subvalue = value.getFirstChild(); subvalue != null; subvalue = subvalue.getNextSibling()) { 560 node_name = subvalue.getNodeName(); 561 if(node_name.equals("Name")) { 562 key = XMLTools.getValue(subvalue); 563 } 564 else if(node_name.equals("Desc")) { 565 desc = XMLTools.getValue(subvalue); 566 } 567 } 568 if(key != null) { 569 addOption(key, desc); 570 } 571 } 572 } 573 } 574 else if(node_name.equals("Range")) { 575 String range_raw = XMLTools.getValue(node); 576 int index = -1; 577 if((index = range_raw.indexOf(StaticStrings.COMMA_CHARACTER)) != -1) { 578 if(index > 0) { 579 try { 580 String first_number = range_raw.substring(0, index); 581 setMinimum(Integer.parseInt(first_number)); 582 first_number = null; 583 } 584 catch(Exception exception) { 585 } 586 } 587 588 if(index + 1 < range_raw.length()) { 589 try { 590 String second_number = range_raw.substring(index + 1); 591 setMaximum(Integer.parseInt(second_number)); 592 second_number = null; 593 } 594 catch(Exception exception) { 595 } 596 } 597 } 598 // Else it wasn't a valid range anyway, so ignore it 599 } 600 else if(node_name.equals("HiddenGLI")) { 601 setHiddenGLI(true); 602 } 603 else if(node_name.equals("ModeGLI")) { 604 String mode_level_str = XMLTools.getValue(node); 605 try { 606 int mode_level = Integer.parseInt(mode_level_str); 607 setModeLevel(mode_level); 608 } 609 catch(Exception exception) { 610 DebugStream.println("Exception in Argument.parseXML() - Unexpected but non-fatal"); 611 DebugStream.printStackTrace(exception); 612 } 613 } 614 615 } // for each option 53 implements Comparable, Serializable { 54 /** An element of the argument type enumeration specifying a combobox control. */ 55 static final public byte ENUM = 0; 56 /** An element of the argument type enumeration specifying a checkbox control. */ 57 static final public byte FLAG = 1; 58 /** An element of the argument type enumeration specifying a tree control. */ 59 static final public byte HIERARCHY = 2; 60 /** An element of the argument type enumeration specifying a spinner control. */ 61 static final public byte INTEGER = 3; 62 /** An element of the argument type enumeration specifying a language combobox control. */ 63 static final public byte LANGUAGE = 4; 64 /** An element of the argument type enumeration specifying a list control. */ 65 static final public byte METADATA = 5; 66 /** An element of the argument type enumeration specifying a metadata combobox control. */ 67 static final public byte METADATUM = 6; 68 /** An element of the argument type enumeration specifying a text field. */ 69 static final public byte STRING = 7; 70 /** An element of the argument type enumeration specifying a regular expression text field. */ 71 static final public byte REGEXP = 8; 72 /** An element of the argument type enumeration specifying a metadata set combobox control. */ 73 static final public byte METADATA_SET_NAMESPACE = 9; 74 75 ///////////kk added the number was 9, I changed it to 10////////////// 76 /** An element of the argument type enumeration specifying a text field. */ 77 static final public byte URL = 10; 78 ///////////////////////////////////////////////////////////////// 79 80 /** true if this argument should actually be hidden within the GLI. This is important for arguments such as import dir or other location critical arguments. */ 81 private boolean hidden_gli = false; 82 /** <i>true</i> if this argument is required for the applicable script to work properly, <i>false</i> otherwise. */ 83 private boolean required = false; 84 /** The type of this argument. Used to be an int, but bytes are cheaper. */ 85 private byte type = STRING; 86 /** The maximum value an integer based control can have. */ 87 private int maximum = Integer.MAX_VALUE; 88 /** The minimum value an integer based control can have. */ 89 private int minimum = Integer.MIN_VALUE; 90 /** Every argument has a detail mode level at which it becomes available to the user to edit. 91 * @see org.greenstone.gatherer.Configuration 92 */ 93 private int mode_level = Configuration.LIBRARIAN_MODE; 94 /** The DOM element this argument is built around, if any. */ 95 private Element element; 96 /** If the argument is of type ENUM then this map holds all the various options. Each entry is an <option value> -> <description> mapping. */ 97 private ArrayList option_list = null; 98 /** A default value for parameter-type arguments. May be a Perl pattern. */ 99 private String default_value = null; 100 /** The text description of this argument parsed from the pluginfo output. */ 101 private String description = null; 102 /** The argument flag as it appears in the command. Also used as the unique identifier of an argument. */ 103 private String name = null; 104 /** The plugin that owns this argument, for the purposes of visualising inheritance. */ 105 private String owner = null; 106 107 private String display_name = null; 108 109 /** Default Constructor. */ 110 public Argument() { 111 } 112 113 /** Another constructor but this one is a little more interesting as it takes a DOM element. 114 * @param element the Element this argument is based around 115 */ 116 public Argument(Element element) { 117 this.element = element; 118 } 119 120 /** Method to add an element to the option_list. 121 * @param name the name value of the option as a String 122 * @param desc the description of this options as a String 123 */ 124 public void addOption(String name, String desc) { 125 if(type == ENUM && name != null) { 126 if(desc == null) { 127 desc = ""; 128 } 129 if(option_list == null) { 130 option_list = new ArrayList(); 131 } 132 option_list.add(new ArgumentOption(name, desc)); 133 } 134 } 135 136 /** Method to compare two arguments for ordering. 137 * @param object the argument we are comparing to, as an Object 138 * @return an int specifying the argument order, using values as set out in String 139 * @see org.greenstone.gatherer.cdm.Argument 140 */ 141 public int compareTo(Object object) { 142 if(object instanceof Argument) { 143 return getName().compareTo(((Argument)object).getName()); 144 } 145 else { 146 return toString().compareTo(object.toString()); 147 } 148 } 149 150 /** Create a copy of this argument. 151 * @return a newly created Argument with the same details as this one 152 */ 153 public Argument copy() { 154 Argument copy = new Argument(); 155 copy.setDefaultValue(default_value); 156 copy.setDescription(description); 157 copy.setOptions(option_list); 158 copy.setOwner(owner); 159 copy.setName(name); 160 copy.setDispalyName(display_name); 161 copy.setRequired(required); 162 copy.setType(type); 163 copy.setMinimum(minimum); 164 copy.setMaximum(maximum); 165 copy.setModeLevel(mode_level); 166 copy.setHiddenGLI(hidden_gli); 167 return copy; 168 } 169 170 /** Method to determine if two arguments are equal. 171 * @param object the argument to test against, as an Object 172 * @return true if the arguments names match, false otherwise 173 */ 174 public boolean equals(Object object) { 175 return (compareTo(object) == 0); 176 } 177 178 /** Method to retrieve the value of default_value. 179 * @return a String containing the default value 180 */ 181 public String getDefaultValue() { 182 return default_value; 183 } 184 185 /** Method to retrieve this arguments description. 186 * @return a String containing the description 187 */ 188 public String getDescription() { 189 return description; 190 } 191 192 public Element getElement() { 193 return element; 194 } 195 /** Retrieve the upper bound of a range based argument. 196 * @return the maximum as an int 197 */ 198 public int getMaximum() { 199 return maximum; 200 } 201 202 /** Retrieve the lower bound of a range based argument. 203 * @return the minimum as an int 204 */ 205 public int getMinimum() { 206 return minimum; 207 } 208 209 /** Retrieves the mode level at which this argument should become available. Any higher levels should also see this argument. 210 * @return the mode level as an int 211 */ 212 public int getModeLevel() { 213 return mode_level; 214 } 215 216 /** Method to retrieve the value of name. 217 * @return a String containing the argument name 218 * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE 219 */ 220 public String getName() { 221 if(name == null && element != null) { 222 name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE); 223 } 224 return name; 225 } 226 227 public String getDisplayName() { 228 if(display_name==null) 229 return ""; 230 return display_name; 231 } 232 233 /** Method to retrieve the option list for this argument. 234 * @return a HashMap containing <option value> -> <description> entries 235 */ 236 public ArrayList getOptions() { 237 return option_list; 238 } 239 240 /** Retrieve the name of the owner of this argument. 241 * @return the owners name as a String 242 */ 243 public String getOwner() { 244 return owner; 245 } 246 247 /** Method to determine the type of this argument. 248 * @return a byte specifying the type 249 */ 250 public byte getType() { 251 return type; 252 } 253 254 /** Method to retrieve the value of value. 255 * @return the value of value as a String 256 * @see org.greenstone.gatherer.Gatherer#c_man 257 * @see org.greenstone.gatherer.collection.CollectionManager#getCollection 258 */ 259 public String getValue() 260 { 261 // Only assigned arguments have values. 262 if (element == null) { 263 return null; 264 } 265 266 return XMLTools.getValue(element); 267 } 268 269 270 /** Method to determine if this argument has been assigned. 271 * @return true if it has, false otherwise 272 * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE 273 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR 274 */ 275 public boolean isAssigned() { 276 return (element != null && element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.TRUE_STR)); 277 } 278 279 /** Determine if this is a custom argument ie one that has been parsed from the config file but doesn't have a matching entry in the argument library. 280 * @return true if this argument is a custom, false otherwise 281 * @see org.greenstone.gatherer.util.StaticStrings#CUSTOM_ATTRIBUTE 282 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR 283 */ 284 public boolean isCustomArgument() { 285 return (element != null && element.getAttribute(StaticStrings.CUSTOM_ATTRIBUTE).equals(StaticStrings.TRUE_STR)); 286 } 287 288 /** Determine if this argument is hidden in GLI 289 * @return true if the argument is hidden, false otherwise 290 */ 291 public boolean isHiddenGLI() { 292 return hidden_gli; 293 } 294 295 /** Method to determine of this argument is required for the associated script to work. 296 * @return true if this argument is required, false otherwise 297 */ 298 public boolean isRequired() { 299 return required; 300 } 301 302 /** Method to allow for the activation of arguments that might never have their setValue() method called. 303 * @param assigned the desired state as a boolean 304 * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE 305 * @see org.greenstone.gatherer.util.StaticStrings#FALSE_STR 306 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR 307 */ 308 public void setAssigned(boolean assigned) { 309 if(element != null) { 310 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR)); 311 } 312 } 313 314 /** Sets the value of default_value. 315 * @param default_value The new value for default_value as a <strong>String</strong>. 316 */ 317 public void setDefaultValue(String default_value) { 318 this.default_value = default_value; 319 } 320 321 /** Set the value of desc. 322 * @param description the new value of desc as a String 323 */ 324 public void setDescription(String description) { 325 this.description = description; 326 } 327 328 /** Set the element this argument should be based upon. 329 * @param element the Element 330 */ 331 public void setElement(Element element) { 332 this.element = element; 333 } 334 335 /** Mark this argument as being hidden in GLI. */ 336 public void setHiddenGLI(boolean hidden) { 337 this.hidden_gli = hidden; 338 } 339 340 /** Set the upper bound for a range type argument. 341 * @param maximum the maximum as an int 342 */ 343 public void setMaximum(int maximum) { 344 this.maximum = maximum; 345 } 346 347 /** Set the lower bound for a range type argument. 348 * @param minimum the minimum as an int 349 */ 350 public void setMinimum(int minimum) { 351 this.minimum = minimum; 352 } 353 354 /** Set the detail mode level where this argument will become available. 355 * @param mode_level the mode level as an int 356 */ 357 public void setModeLevel(int mode_level) { 358 this.mode_level = mode_level; 359 } 360 361 /** Set the value of name. 362 * @param name the new value of name as a String 363 */ 364 public void setName(String name) { 365 this.name = name; 366 } 616 367 617 } // parseXML 618 619 public class ArgumentOption 368 public void setDispalyName(String name) { 369 this.display_name = name; 370 } 371 372 /** Sets the value of the options list. 373 * @param list the new options list as a HashMap 374 */ 375 public void setOptions(ArrayList list) { 376 this.option_list = list; 377 } 378 379 /** Set the owner of this argument. 380 * @param owner the name of the owner of this argument as a String 381 */ 382 public void setOwner(String owner) { 383 this.owner = owner; 384 } 385 386 /** Set the value of required. 387 * @param required the new value of required as a boolean 388 */ 389 public void setRequired(boolean required) { 390 this.required = required; 391 } 392 393 /** Set the value of type. 394 * @param type the new value of type as an byte 395 */ 396 public void setType(byte type) { 397 this.type = type; 398 } 399 400 /** Set the value of type, by matching a type to the given string. 401 * @param new_type a String which contains the name of a certain argument type 402 * @see org.greenstone.gatherer.util.StaticStrings#ENUM_STR 403 * @see org.greenstone.gatherer.util.StaticStrings#FLAG_STR 404 * @see org.greenstone.gatherer.util.StaticStrings#HIERARCHY_STR 405 * @see org.greenstone.gatherer.util.StaticStrings#INT_STR 406 * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGE_STR 407 * @see org.greenstone.gatherer.util.StaticStrings#METADATA_TYPE_STR 408 * @see org.greenstone.gatherer.util.StaticStrings#METADATUM_TYPE_STR 409 * @see org.greenstone.gatherer.util.StaticStrings#REGEXP_STR 410 */ 411 public void setType(String new_type) { 412 if(new_type.equalsIgnoreCase(StaticStrings.ENUM_STR)) { 413 this.type = ENUM; 414 option_list = new ArrayList(); 415 } 416 else if(new_type.equalsIgnoreCase(StaticStrings.FLAG_STR)) { 417 this.type = FLAG; 418 } 419 else if(new_type.equalsIgnoreCase(StaticStrings.HIERARCHY_STR)) { 420 this.type = HIERARCHY; 421 } 422 else if(new_type.equalsIgnoreCase(StaticStrings.INT_STR)) { 423 this.type = INTEGER; 424 } 425 else if(new_type.equalsIgnoreCase(StaticStrings.LANGUAGE_STR)) { 426 this.type = LANGUAGE; 427 } 428 else if(new_type.equalsIgnoreCase(StaticStrings.METADATA_TYPE_STR)) { 429 this.type = METADATA; 430 } 431 else if(new_type.equalsIgnoreCase(StaticStrings.METADATUM_TYPE_STR)) { 432 this.type = METADATUM; 433 } 434 else if(new_type.equalsIgnoreCase(StaticStrings.REGEXP_STR)) { 435 this.type = REGEXP; 436 } 437 else { 438 this.type = STRING; 439 } 440 } 441 442 /** Method to set the value of this argument. 443 * @param value the new value for the argument 444 * @see org.greenstone.gatherer.Gatherer#println 445 */ 446 public void setValue(String value) { 447 if(element != null) { 448 XMLTools.setValue(element, value); 449 } 450 else { 451 DebugStream.println("Argument.setValue(" + value + ") called on a base Argument."); 452 } 453 } 454 455 /** Set the values vector to the given values. Currently I just assign the new values, whereas I may later want to implement a deep clone. 456 * @param values an ArrayList of values 457 * @see org.greenstone.gatherer.Gatherer#println 458 */ 459 public void setValues(ArrayList values) { 460 if(element != null) { 461 StringBuffer value = new StringBuffer(); 462 int value_length = values.size(); 463 for(int i = 0; i < value_length; i++) { 464 value.append(values.get(i)); 465 value.append(StaticStrings.COMMA_CHARACTER); 466 } 467 value.deleteCharAt(value.length() - 1); // Remove last ',' 468 XMLTools.setValue(element, value.toString()); 469 } 470 else { 471 DebugStream.println("Argument.setValues([" + values.size() + " items]) called on a base Argument."); 472 } 473 } 474 475 /** Method for translating the data of this class into a string. 476 * @return a String containing a fragment of the total arguments string 477 * @see org.greenstone.gatherer.Gatherer#c_man 478 * @see org.greenstone.gatherer.collection.CollectionManager#getCollection 479 * @see org.greenstone.gatherer.util.StaticStrings#COMMA_CHARACTER 480 * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE 481 * @see org.greenstone.gatherer.util.StaticStrings#SPACE_CHARACTER 482 * @see org.greenstone.gatherer.util.StaticStrings#SPEECH_CHARACTER 483 */ 484 public String toString() 485 { 486 StringBuffer text = new StringBuffer("-"); 487 488 if (element == null) { 489 return text.toString(); 490 } 491 492 if (name == null) { 493 name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE); 494 } 495 text.append(name); 496 497 String value = XMLTools.getValue(element); 498 if (value.length() == 0) { 499 return text.toString(); 500 } 501 502 text.append(StaticStrings.SPACE_CHARACTER); 503 504 // // Handle metadata elements specially 505 // if (type == METADATA || type == METADATUM) { 506 // // Tokenize the string 507 // StringTokenizer tokenizer = new StringTokenizer(value, ","); 508 // while (tokenizer.hasMoreTokens()) { 509 // String token = tokenizer.nextToken(); 510 511 // MetadataElement metadata_element = MetadataTools.getMetadataElementWithDisplayName(token); 512 // if (metadata_element != null) { 513 // text.append(metadata_element.getFullName()); 514 // } 515 // else { 516 // text.append(token); 517 // } 518 519 // if (tokenizer.hasMoreTokens()) { 520 // text.append(StaticStrings.COMMA_CHARACTER); 521 // } 522 // } 523 // return text.toString(); 524 // } 525 526 // If the value contains a space, add speech marks 527 // (Except for metadata elements, which won't have spaces when written out to collect.cfg) 528 if (value.indexOf(StaticStrings.SPACE_CHARACTER) != -1 && !(type == METADATUM || type == METADATA)) { 529 value = StaticStrings.SPEECH_CHARACTER + value + StaticStrings.SPEECH_CHARACTER; 530 } 531 532 text.append(value); 533 return text.toString(); 534 } 535 536 /** parse the <Option> XML from eg import.pl -xml or pluginfo.pl -xml */ 537 public void parseXML(Element option) { 538 539 for(Node node = option.getFirstChild(); node != null; node = node.getNextSibling()) { 540 String node_name = node.getNodeName(); 541 if(node_name.equals("Name")) { 542 setName(XMLTools.getValue(node)); 543 } 544 else if(node_name.equals("DisplayName")) { 545 setDispalyName(XMLTools.getValue(node)); 546 } 547 else if(node_name.equals("Desc")) { 548 setDescription(XMLTools.getValue(node)); 549 } 550 else if(node_name.equals("Type")) { 551 setType(XMLTools.getValue(node)); 552 } 553 else if(node_name.equals("Default")) { 554 setDefaultValue(XMLTools.getValue(node)); 555 } 556 else if(node_name.equals("Required")) { 557 String v = XMLTools.getValue(node); 558 if(v != null && v.equals("yes")) { 559 setRequired(true); 560 } 561 } 562 else if(node_name.equals("List")) { 563 // Two final loops are required to parse lists. 564 for(Node value = node.getFirstChild(); value != null; value = value.getNextSibling()) { 565 if(value.getNodeName().equals("Value")) { 566 String key = null; 567 String desc = ""; 568 for(Node subvalue = value.getFirstChild(); subvalue != null; subvalue = subvalue.getNextSibling()) { 569 node_name = subvalue.getNodeName(); 570 if(node_name.equals("Name")) { 571 key = XMLTools.getValue(subvalue); 572 } 573 else if(node_name.equals("Desc")) { 574 desc = XMLTools.getValue(subvalue); 575 } 576 } 577 if(key != null) { 578 addOption(key, desc); 579 } 580 } 581 } 582 } 583 else if(node_name.equals("Range")) { 584 String range_raw = XMLTools.getValue(node); 585 int index = -1; 586 if((index = range_raw.indexOf(StaticStrings.COMMA_CHARACTER)) != -1) { 587 if(index > 0) { 588 try { 589 String first_number = range_raw.substring(0, index); 590 setMinimum(Integer.parseInt(first_number)); 591 first_number = null; 592 } 593 catch(Exception exception) { 594 } 595 } 596 597 if(index + 1 < range_raw.length()) { 598 try { 599 String second_number = range_raw.substring(index + 1); 600 setMaximum(Integer.parseInt(second_number)); 601 second_number = null; 602 } 603 catch(Exception exception) { 604 } 605 } 606 } 607 // Else it wasn't a valid range anyway, so ignore it 608 } 609 else if(node_name.equals("HiddenGLI")) { 610 setHiddenGLI(true); 611 } 612 else if(node_name.equals("ModeGLI")) { 613 String mode_level_str = XMLTools.getValue(node); 614 try { 615 int mode_level = Integer.parseInt(mode_level_str); 616 setModeLevel(mode_level); 617 } 618 catch(Exception exception) { 619 DebugStream.println("Exception in Argument.parseXML() - Unexpected but non-fatal"); 620 DebugStream.printStackTrace(exception); 621 } 622 } 623 624 } // for each option 625 626 } // parseXML 627 628 public class ArgumentOption 620 629 implements Comparable { 621 public String name;622 public String description;623 private String text; // cached version624 625 public ArgumentOption(String name, String desc) {626 627 628 }629 630 public int compareTo(Object obj) {631 632 }633 634 public boolean equals(Object obj) {635 636 }637 638 public String toString() {639 640 }641 642 public String getToolTip() {643 644 }645 630 public String name; 631 public String description; 632 private String text; // cached version 633 634 public ArgumentOption(String name, String desc) { 635 this.name = name; 636 this.description = desc; 637 } 638 639 public int compareTo(Object obj) { 640 return toString().compareTo(obj.toString()); 641 } 642 643 public boolean equals(Object obj) { 644 return (obj != null && compareTo(obj) == 0); 645 } 646 647 public String toString() { 648 return name + " "+ StaticStrings.MINUS_CHARACTER + " "+ description; 649 } 650 651 public String getToolTip() { 652 return Utility.formatHTMLWidth(name+": "+description, 80); 653 } 654 } 646 655 } -
gli/branches/2.75/src/org/greenstone/gatherer/cdm/ArgumentContainer.java
r12826 r14784 43 43 */ 44 44 abstract public class ArgumentContainer 45 extends ArrayList 46 implements Comparable, DOMProxyListEntry, Serializable { 47 48 // The DOM Element this container is modelled on 49 protected Element element; 50 protected boolean is_abstract = false; 51 protected ArgumentContainer super_container; 52 protected String description; 53 protected String name; 54 55 /** Constructor used in DOMProxyListModel initializations 56 */ 57 public ArgumentContainer() { 58 } 59 60 public ArgumentContainer(Element element, ArgumentContainer base_container) { 61 62 super(); 63 this.element = element; 64 this.name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE); 65 66 // Parse in any argument options for this container, keeping a list of the ones found 67 HashMap known_arguments = new HashMap(); 68 NodeList option_elements = element.getElementsByTagName(StaticStrings.OPTION_ELEMENT); 69 int option_elements_length = option_elements.getLength(); 70 for(int i = 0; i < option_elements_length; i++) { 71 Element option_element = (Element) option_elements.item(i); 72 Argument argument = new Argument(option_element); 73 ///ystem.err.println("Rebuilding existing argument: " + argument.getName()); 74 known_arguments.put(argument.getName(), argument); 75 } 76 // If a base classifier was given 77 if(base_container != null) { 78 // Copy the details, and add a reference to whatever the base container's super container is 79 description = base_container.getDescription(); 80 // Now search through the 'dummy' arguments belonging to the base container. For each found, if it is already assigned, fill out further details such as type. If any are found that are not already assigned for this container, copy them and add them, but without a value. 81 ArrayList all_arguments = base_container.getArguments(); 82 int argument_count = all_arguments.size(); 83 for(int j = 0; j < argument_count; j++) { 84 Argument base_argument = (Argument) all_arguments.get(j); 85 String base_argument_name = base_argument.getName(); 86 ///ystem.err.println("Library indicates this container should have an argument: " + base_argument_name); 87 Argument existing_argument = (Argument) known_arguments.get(base_argument_name); 88 // Found an existing argument. Complete its details 89 if(existing_argument != null) { 90 ///ystem.err.println("Found existing argument. Filling out details."); 91 known_arguments.remove(base_argument_name); 92 existing_argument.setDefaultValue(base_argument.getDefaultValue()); 93 existing_argument.setDescription(base_argument.getDescription()); 94 existing_argument.setOptions(base_argument.getOptions()); 95 existing_argument.setRequired(base_argument.isRequired()); 96 existing_argument.setType(base_argument.getType()); 97 existing_argument.setMinimum(base_argument.getMinimum()); 98 existing_argument.setMaximum(base_argument.getMaximum()); 99 existing_argument.setOwner(base_argument.getOwner()); 100 existing_argument.setHiddenGLI(base_argument.isHiddenGLI()); 101 add(existing_argument); 102 } 103 // No existing argument. Copy base_argument and add it, but set its assigned flag to false 104 else { 105 ///atherer.println("No such argument. Adding new, unassigned, argument."); 106 // The trick thing is that we have to create a new element in the DOM as well. 107 Argument new_argument = base_argument.copy(); 108 Element argument_element = CollectionConfiguration.createElement(StaticStrings.OPTION_ELEMENT); 109 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, base_argument_name); 110 argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR); 111 new_argument.setElement(argument_element); 112 // All done. Add it. 113 element.appendChild(argument_element); 114 add(new_argument); 115 } 116 } 117 } 118 119 // The first time we do this, the args come from the config file and 120 // may contain invalid args. Here we remove them from the DOM. 121 if (!known_arguments.isEmpty()) { 122 Set entry_set = known_arguments.entrySet(); 123 Iterator entry_iterator = entry_set.iterator(); 124 while (entry_iterator.hasNext()) { 125 Argument arg = (Argument)((Map.Entry)entry_iterator.next()).getValue(); 126 Element e = arg.getElement(); 127 element.removeChild(e); 128 } 129 } 130 } 131 132 133 134 135 /** Constructor used for the plugins that are in the DOMProxyList */ 136 /** Method to add an argument to this container. Only adds the argument if it isn't already present. 137 * @param argument The <strong>Argument</strong> to add. 138 */ 139 public void addArgument(Argument argument) { 140 if(element == null && !contains(argument)) { 141 add(argument); 142 argument.setOwner(name); 143 } 144 } 145 146 /** Method to compare two containers for ordering. 147 * @param object The container we are comparing to, as an <strong>Object</strong>. 148 * @return An <i>int</i> specifying the container order, using values as set out in String. 149 * @see java.lang.String#compareTo 150 */ 151 public int compareTo(Object object) { 152 if(object == null ) { 153 return -1; 154 } 155 return toString().compareTo(object.toString()); 156 } 157 158 /** Method to determine if two containers are equal. 159 * @param object The container to test against, as an <strong>Object</strong>. 160 * @return <i>true</i> if they match (based on the equals method), <i>false</i> otherwise. 161 */ 162 163 public boolean equals(Object object) { 164 return (compareTo(object) == 0); 165 } 166 167 abstract public DOMProxyListEntry create(Element element); 168 /** Method to retrieve an argument by its name. 169 * @param name The name of the argument as a <strong>String</strong>. 170 * @return The <strong>Argument</strong> requested, or <i>null</i> if no such argument. 171 */ 172 public Argument getArgument(String name) { 173 // The name given may still include the '-' 174 if(name.startsWith("-")) { 175 name = name.substring(1); 176 } 177 //ArrayList arguments = getArguments(true, true); 178 ArrayList arguments = getArguments(); 179 for(int i = 0; i < arguments.size(); i++) { 180 Argument argument = (Argument)arguments.get(i); 181 if(argument.getName().equals(name)) { 182 return argument; 183 } 184 } 185 return null; 186 } 187 188 /** Method to retrieve the list of arguments from this container. Note that this method returns both the containers arguments plus its 'supers' arguments if any, and alphabetically orders them. 189 * @return the arguments within a ArrayList 190 */ 191 public ArrayList getArguments() { 192 ArrayList arguments = new ArrayList(); 193 arguments.addAll(this); 194 if(super_container != null) { 195 ArrayList remainder = super_container.getArguments(); 196 remainder.removeAll(arguments); 197 arguments.addAll(remainder); 198 } 199 return arguments; 200 201 } 202 203 public String getDescription() { 204 return description; 205 } 206 207 public Element getElement() { 208 return element; 209 } 210 /** Method to retrieve the name associated with this argument container. 211 * @return the name as a String 212 */ 213 public String getName() { 214 if(name == null && element != null) { 215 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE); 216 } 217 return name; 218 } 219 220 public boolean isAbstract() { 221 return is_abstract; 222 } 223 224 public boolean isAssigned() { 225 return (element != null && !element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR)); 226 } 227 228 public void setAssigned(boolean assigned) { 229 if(element != null) { 230 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR)); 231 } 232 } 233 234 public void setDescription(String description) { 235 this.description = description; 236 } 237 238 public void setElement(Element element) { 239 this.element = element; 240 } 241 242 public void setIsAbstract(boolean is_abstract) { 243 this.is_abstract = is_abstract; 244 } 245 246 public void setName(String name) { 247 this.name = name; 248 } 249 250 /** Method to set the value of the super_container. 251 * @param super_container The new value of super_container as a <strong>ArgumentContainer</strong>, or <i>null</i> if this class has no inheritance. 252 */ 253 public void setSuper(ArgumentContainer super_container) { 254 this.super_container = super_container; 255 } 256 257 public String toString() 258 { 259 if (element == null) { 260 return name; 261 } 262 263 if (name == null) { 264 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE); 265 } 266 267 StringBuffer text = new StringBuffer(" "); 268 if (!isAssigned()) { 269 text.append("#"); 270 } 271 text.append(name); 272 273 ArrayList arguments = getArguments(); 274 for (int i = 0; i < arguments.size(); i++) { 275 Argument argument = (Argument) arguments.get(i); 276 if (argument.isAssigned()) { 277 text.append(" "); 278 text.append(argument.toString()); 279 } 280 } 281 282 return text.toString(); 283 } 284 285 public ArrayList getArguments(boolean include_normal, boolean include_custom){ 286 return this; 287 } 45 extends ArrayList 46 implements Comparable, DOMProxyListEntry, Serializable { 47 48 // The DOM Element this container is modelled on 49 protected Element element; 50 protected boolean is_abstract = false; 51 protected ArgumentContainer super_container; 52 protected String description; 53 protected String name; 54 55 /** Constructor used in DOMProxyListModel initializations 56 */ 57 public ArgumentContainer() { 58 } 59 60 public ArgumentContainer(Element element, ArgumentContainer base_container) { 61 62 super(); 63 this.element = element; 64 this.name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE); 65 66 // Parse in any argument options for this container, keeping a list of the ones found 67 HashMap known_arguments = new HashMap(); 68 NodeList option_elements = element.getElementsByTagName(StaticStrings.OPTION_ELEMENT); 69 int option_elements_length = option_elements.getLength(); 70 for(int i = 0; i < option_elements_length; i++) { 71 Element option_element = (Element) option_elements.item(i); 72 Argument argument = new Argument(option_element); 73 ///ystem.err.println("Rebuilding existing argument: " + argument.getName()); 74 known_arguments.put(argument.getName(), argument); 75 } 76 // If a base classifier was given 77 if(base_container != null) { 78 // Copy the details, and add a reference to whatever the base container's super container is 79 description = base_container.getDescription(); 80 // Now search through the 'dummy' arguments belonging to the base container. For each found, if it is already assigned, fill out further details such as type. If any are found that are not already assigned for this container, copy them and add them, but without a value. 81 ArrayList all_arguments = base_container.getArguments(); 82 int argument_count = all_arguments.size(); 83 for(int j = 0; j < argument_count; j++) { 84 Argument base_argument = (Argument) all_arguments.get(j); 85 String base_argument_name = base_argument.getName(); 86 ///ystem.err.println("Library indicates this container should have an argument: " + base_argument_name); 87 Argument existing_argument = (Argument) known_arguments.get(base_argument_name); 88 // Found an existing argument. Complete its details 89 if(existing_argument != null) { 90 ///ystem.err.println("Found existing argument. Filling out details."); 91 known_arguments.remove(base_argument_name); 92 existing_argument.setDispalyName(base_argument.getDisplayName()); 93 existing_argument.setDefaultValue(base_argument.getDefaultValue()); 94 existing_argument.setDescription(base_argument.getDescription()); 95 existing_argument.setOptions(base_argument.getOptions()); 96 existing_argument.setRequired(base_argument.isRequired()); 97 existing_argument.setType(base_argument.getType()); 98 existing_argument.setMinimum(base_argument.getMinimum()); 99 existing_argument.setMaximum(base_argument.getMaximum()); 100 existing_argument.setOwner(base_argument.getOwner()); 101 existing_argument.setHiddenGLI(base_argument.isHiddenGLI()); 102 add(existing_argument); 103 } 104 // No existing argument. Copy base_argument and add it, but set its assigned flag to false 105 else { 106 ///atherer.println("No such argument. Adding new, unassigned, argument."); 107 // The trick thing is that we have to create a new element in the DOM as well. 108 Argument new_argument = base_argument.copy(); 109 Element argument_element = CollectionConfiguration.createElement(StaticStrings.OPTION_ELEMENT); 110 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, base_argument_name); 111 argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR); 112 new_argument.setElement(argument_element); 113 // All done. Add it. 114 element.appendChild(argument_element); 115 add(new_argument); 116 } 117 } 118 } 119 120 // The first time we do this, the args come from the config file and 121 // may contain invalid args. Here we remove them from the DOM. 122 if (!known_arguments.isEmpty()) { 123 Set entry_set = known_arguments.entrySet(); 124 Iterator entry_iterator = entry_set.iterator(); 125 while (entry_iterator.hasNext()) { 126 Argument arg = (Argument)((Map.Entry)entry_iterator.next()).getValue(); 127 Element e = arg.getElement(); 128 element.removeChild(e); 129 } 130 } 131 } 132 133 134 135 136 /** Constructor used for the plugins that are in the DOMProxyList */ 137 /** Method to add an argument to this container. Only adds the argument if it isn't already present. 138 * @param argument The <strong>Argument</strong> to add. 139 */ 140 public void addArgument(Argument argument) { 141 if(element == null && !contains(argument)) { 142 add(argument); 143 argument.setOwner(name); 144 } 145 } 146 147 /** Method to compare two containers for ordering. 148 * @param object The container we are comparing to, as an <strong>Object</strong>. 149 * @return An <i>int</i> specifying the container order, using values as set out in String. 150 * @see java.lang.String#compareTo 151 */ 152 public int compareTo(Object object) { 153 if(object == null ) { 154 return -1; 155 } 156 return toString().compareTo(object.toString()); 157 } 158 159 /** Method to determine if two containers are equal. 160 * @param object The container to test against, as an <strong>Object</strong>. 161 * @return <i>true</i> if they match (based on the equals method), <i>false</i> otherwise. 162 */ 163 164 public boolean equals(Object object) { 165 return (compareTo(object) == 0); 166 } 167 168 abstract public DOMProxyListEntry create(Element element); 169 /** Method to retrieve an argument by its name. 170 * @param name The name of the argument as a <strong>String</strong>. 171 * @return The <strong>Argument</strong> requested, or <i>null</i> if no such argument. 172 */ 173 public Argument getArgument(String name) { 174 // The name given may still include the '-' 175 if(name.startsWith("-")) { 176 name = name.substring(1); 177 } 178 //ArrayList arguments = getArguments(true, true); 179 ArrayList arguments = getArguments(); 180 for(int i = 0; i < arguments.size(); i++) { 181 Argument argument = (Argument)arguments.get(i); 182 if(argument.getName().equals(name)) { 183 return argument; 184 } 185 } 186 return null; 187 } 188 189 /** Method to retrieve the list of arguments from this container. Note that this method returns both the containers arguments plus its 'supers' arguments if any, and alphabetically orders them. 190 * @return the arguments within a ArrayList 191 */ 192 public ArrayList getArguments() { 193 ArrayList arguments = new ArrayList(); 194 arguments.addAll(this); 195 if(super_container != null) { 196 ArrayList remainder = super_container.getArguments(); 197 remainder.removeAll(arguments); 198 arguments.addAll(remainder); 199 } 200 return arguments; 201 202 } 203 204 public String getDescription() { 205 return description; 206 } 207 208 public Element getElement() { 209 return element; 210 } 211 /** Method to retrieve the name associated with this argument container. 212 * @return the name as a String 213 */ 214 public String getName() { 215 if(name == null && element != null) { 216 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE); 217 } 218 return name; 219 } 220 221 public boolean isAbstract() { 222 return is_abstract; 223 } 224 225 public boolean isAssigned() { 226 return (element != null && !element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR)); 227 } 228 229 public void setAssigned(boolean assigned) { 230 if(element != null) { 231 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR)); 232 } 233 } 234 235 public void setDescription(String description) { 236 this.description = description; 237 } 238 239 public void setElement(Element element) { 240 this.element = element; 241 } 242 243 public void setIsAbstract(boolean is_abstract) { 244 this.is_abstract = is_abstract; 245 } 246 247 public void setName(String name) { 248 this.name = name; 249 } 250 251 /** Method to set the value of the super_container. 252 * @param super_container The new value of super_container as a <strong>ArgumentContainer</strong>, or <i>null</i> if this class has no inheritance. 253 */ 254 public void setSuper(ArgumentContainer super_container) { 255 this.super_container = super_container; 256 } 257 258 public String toString() 259 { 260 if (element == null) { 261 return name; 262 } 263 264 if (name == null) { 265 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE); 266 } 267 268 StringBuffer text = new StringBuffer(" "); 269 if (!isAssigned()) { 270 text.append("#"); 271 } 272 text.append(name); 273 274 ArrayList arguments = getArguments(); 275 for (int i = 0; i < arguments.size(); i++) { 276 Argument argument = (Argument) arguments.get(i); 277 if (argument.isAssigned()) { 278 text.append(" "); 279 text.append(argument.toString()); 280 } 281 } 282 283 return text.toString(); 284 } 285 286 public ArrayList getArguments(boolean include_normal, boolean include_custom){ 287 return this; 288 } 288 289 } -
gli/branches/2.75/src/org/greenstone/gatherer/cdm/ArgumentControl.java
r13158 r14784 26 26 /** This class encapsulates all the technical difficulty of creating a specific control based on an Argument. */ 27 27 public class ArgumentControl 28 extends JPanel { 29 30 static protected Dimension LABEL_SIZE = new Dimension(175, 25); 31 static protected Dimension ROW_SIZE = new Dimension(400, 30); 32 /** The Argument this control will be based on. */ 33 private Argument argument = null; 34 35 /** A checkbox to allow enabling or diabling of this Argument. */ 36 private JCheckBox enabled = null; 37 /** Some form of editor component, such as a JComboBox or JTextField, used to set parameters to an Argument. */ 38 private JComponent value_control = null; 39 40 /** Constructor. 41 */ 42 public ArgumentControl(Argument argument, boolean is_enabled, String preset_value) { 43 this.argument = argument; 44 45 String tip = "<html>" + argument.getName()+": "+argument.getDescription() + "</html>"; 46 tip = Utility.formatHTMLWidth(tip, 80); 47 48 setBackground(Configuration.getColor("coloring.collection_tree_background", false)); 49 setBorder(BorderFactory.createEmptyBorder(2,0,2,0)); 50 setLayout(new BorderLayout()); 51 setPreferredSize(ROW_SIZE); 52 53 if (argument.isRequired()) { 54 JLabel label = new JLabel(argument.getName()); 55 label.setOpaque(false); 56 label.setPreferredSize(LABEL_SIZE); 57 label.setToolTipText(tip); 58 add(label, BorderLayout.WEST); 59 } else { 60 enabled = new JCheckBox(argument.getName()); 61 enabled.setOpaque(false); 62 enabled.setPreferredSize(LABEL_SIZE); 63 enabled.setToolTipText(tip); 64 add(enabled, BorderLayout.WEST); 65 } 66 67 String initial_value; 68 if (preset_value != null && !preset_value.equals("")) { 69 initial_value = preset_value; 70 } 71 else { 72 initial_value = argument.getValue(); 73 } 74 if (initial_value == null || initial_value.equals("")) { 75 initial_value = argument.getDefaultValue(); 76 } 77 if (initial_value == null) { 78 initial_value = ""; 79 } 80 81 switch(argument.getType()) { 82 case Argument.ENUM: 83 ArrayList option_list = argument.getOptions(); 84 value_control = new GComboBox(option_list.toArray(), false, false); 85 selectValue((JComboBox)value_control, initial_value); // also sets the tooltip 86 ((JComboBox)value_control).addActionListener(new ToolTipUpdater()); 87 break; 88 89 case Argument.FLAG: 90 // Only need the check box. 91 break; 92 93 case Argument.INTEGER: 94 // Build a spinner 95 int initial_int=0; 96 // If there was an original value, set it. 97 try { 98 initial_int = Integer.parseInt(initial_value); 99 } catch (Exception error) { 100 DebugStream.println("ArgumentControl Error: "+error); 101 } 102 if (initial_int < argument.getMinimum()) { 103 initial_int = argument.getMinimum(); 104 } else if (initial_int > argument.getMaximum()) { 105 initial_int = argument.getMaximum(); 106 } 107 108 JSpinner spinner = new JSpinner(new SpinnerNumberModel(initial_int, argument.getMinimum(), argument.getMaximum(), 1)); 109 110 // And remember it 111 value_control = spinner; 112 break; 113 114 case Argument.REGEXP: 115 case Argument.STRING: 116 value_control = new JTextField(initial_value); 117 break; 28 extends JPanel { 29 30 static protected Dimension LABEL_SIZE = new Dimension(175, 25); 31 static protected Dimension ROW_SIZE = new Dimension(400, 30); 32 /** The Argument this control will be based on. */ 33 private Argument argument = null; 34 35 /** A checkbox to allow enabling or diabling of this Argument. */ 36 private JCheckBox enabled = null; 37 /** Some form of editor component, such as a JComboBox or JTextField, used to set parameters to an Argument. */ 38 private JComponent value_control = null; 39 40 /** Constructor. 41 */ 42 public ArgumentControl(Argument argument, boolean is_enabled, String preset_value) { 43 this.argument = argument; 44 45 String tip = "<html>" + argument.getName()+": "+argument.getDescription() + "</html>"; 46 tip = Utility.formatHTMLWidth(tip, 80); 47 48 setBackground(Configuration.getColor("coloring.collection_tree_background", false)); 49 setBorder(BorderFactory.createEmptyBorder(2,0,2,0)); 50 setLayout(new BorderLayout()); 51 setPreferredSize(ROW_SIZE); 118 52 119 case Argument.LANGUAGE: 120 value_control = new GComboBox(CollectionDesignManager.language_manager.getLanguageCodes().toArray(), false); 121 // we want to display the language name not the code 122 ((JComboBox)value_control).setRenderer(new LanguageListCellRenderer()); 123 // Now ensure we have the existing value or default value selected if either exist in our known languages 124 String lang_name = CollectionDesignManager.language_manager.getLanguageName(initial_value); 125 if (lang_name != null) { 126 ((JComboBox)value_control).setSelectedItem(initial_value); 127 } 128 break; 129 130 case Argument.METADATUM: 131 case Argument.METADATA: 132 value_control = new GComboBox(MetadataSetManager.getEveryMetadataSetElement(), false); 133 134 // Editable for advanced modes (allows things like dc.Title,ex.Title) 135 if (Configuration.getMode() > Configuration.ASSISTANT_MODE) { 136 ((JComboBox) value_control).setEditable(true); 137 } 138 // Now ensure we have the existing value or default value selected if either exist. 139 String existing_value = preset_value; 140 if (existing_value == null || existing_value.length() == 0) { 141 existing_value = argument.getValue(); 142 } 143 if (existing_value != null && existing_value.length() > 0) { 144 boolean found = selectValue((JComboBox) value_control, existing_value); 145 // It's possible that this is a custom value and so doesn't exist in the combobox 146 if (!found) { 147 // If so, add it then select it 148 ((JComboBox) value_control).addItem(existing_value); 149 ((JComboBox) value_control).setSelectedItem(existing_value); 150 } 151 } 152 else { 153 String default_value = argument.getDefaultValue(); 154 if (default_value != null) { 155 // if no namespace for default value, add ex. 156 // won't work if we want to set a non-metadata value 157 if (MetadataTools.getMetadataSetNamespace(default_value).equals("")) { 158 default_value = StaticStrings.EXTRACTED_NAMESPACE+default_value; 159 } 160 selectValue((JComboBox) value_control, default_value); 161 } 162 } 163 break; 164 165 case Argument.METADATA_SET_NAMESPACE: 166 value_control = new JComboBox(); 167 // !! Hack for exploding metadata databases: add the (empty) exploded metadata set 168 File exploded_metadata_set_file = new File(Gatherer.getGLIMetadataDirectoryPath() + "exp" + StaticStrings.METADATA_SET_EXTENSION); 169 MetadataSet exploded_metadata_set = new MetadataSet(exploded_metadata_set_file); 170 Gatherer.c_man.importMetadataSet(exploded_metadata_set); 171 172 // All the loaded metadata sets except the extracted metadata set are applicable 173 ArrayList metadata_sets = MetadataSetManager.getMetadataSets(); 174 for (int i = metadata_sets.size() - 1; i >= 0; i--) { 175 MetadataSet metadata_set = (MetadataSet) metadata_sets.get(i); 176 if (!(metadata_set.getNamespace().equals(MetadataSetManager.EXTRACTED_METADATA_NAMESPACE))) { 177 ((JComboBox)value_control).addItem(metadata_set); 178 } 179 } 180 181 selectValue((JComboBox) value_control, initial_value); 182 183 } // end of switch 184 185 // Enable or disable as necessary. 186 if(argument.isRequired() || argument.isAssigned() || is_enabled) { 187 if (enabled != null) { 53 // display the name set in the disp option if there is one 54 // otherwise display name option value instead 55 String dispName = argument.getDisplayName(); 56 if(dispName.equals("")){ 57 dispName = argument.getName(); 58 } 59 60 if (argument.isRequired()) { 61 //JLabel label = new JLabel(argument.getName()); 62 JLabel label = new JLabel(dispName); 63 64 label.setOpaque(false); 65 label.setPreferredSize(LABEL_SIZE); 66 label.setToolTipText(tip); 67 add(label, BorderLayout.WEST); 68 } else { 69 //enabled = new JCheckBox(argument.getName()); 70 enabled = new JCheckBox(dispName); 71 72 enabled.setOpaque(false); 73 enabled.setPreferredSize(LABEL_SIZE); 74 enabled.setToolTipText(tip); 75 add(enabled, BorderLayout.WEST); 76 } 77 78 String initial_value; 79 if (preset_value != null && !preset_value.equals("")) { 80 initial_value = preset_value; 81 } 82 else { 83 initial_value = argument.getValue(); 84 } 85 if (initial_value == null || initial_value.equals("")) { 86 initial_value = argument.getDefaultValue(); 87 } 88 if (initial_value == null) { 89 initial_value = ""; 90 } 91 92 switch(argument.getType()) { 93 case Argument.ENUM: 94 ArrayList option_list = argument.getOptions(); 95 value_control = new GComboBox(option_list.toArray(), false, false); 96 selectValue((JComboBox)value_control, initial_value); // also sets the tooltip 97 ((JComboBox)value_control).addActionListener(new ToolTipUpdater()); 98 break; 99 100 case Argument.FLAG: 101 // Only need the check box. 102 break; 103 104 case Argument.INTEGER: 105 // Build a spinner 106 int initial_int=0; 107 // If there was an original value, set it. 108 try { 109 initial_int = Integer.parseInt(initial_value); 110 } catch (Exception error) { 111 DebugStream.println("ArgumentControl Error: "+error); 112 } 113 if (initial_int < argument.getMinimum()) { 114 initial_int = argument.getMinimum(); 115 } else if (initial_int > argument.getMaximum()) { 116 initial_int = argument.getMaximum(); 117 } 118 119 JSpinner spinner = new JSpinner(new SpinnerNumberModel(initial_int, argument.getMinimum(), argument.getMaximum(), 1)); 120 121 // And remember it 122 value_control = spinner; 123 break; 124 125 case Argument.REGEXP: 126 case Argument.STRING: 127 value_control = new JTextField(initial_value); 128 break; 129 130 case Argument.LANGUAGE: 131 value_control = new GComboBox(CollectionDesignManager.language_manager.getLanguageCodes().toArray(), false); 132 // we want to display the language name not the code 133 ((JComboBox)value_control).setRenderer(new LanguageListCellRenderer()); 134 // Now ensure we have the existing value or default value selected if either exist in our known languages 135 String lang_name = CollectionDesignManager.language_manager.getLanguageName(initial_value); 136 if (lang_name != null) { 137 ((JComboBox)value_control).setSelectedItem(initial_value); 138 } 139 break; 140 141 case Argument.METADATUM: 142 case Argument.METADATA: 143 value_control = new GComboBox(MetadataSetManager.getEveryMetadataSetElement(), false); 144 145 // Editable for advanced modes (allows things like dc.Title,ex.Title) 146 if (Configuration.getMode() > Configuration.ASSISTANT_MODE) { 147 ((JComboBox) value_control).setEditable(true); 148 } 149 // Now ensure we have the existing value or default value selected if either exist. 150 String existing_value = preset_value; 151 if (existing_value == null || existing_value.length() == 0) { 152 existing_value = argument.getValue(); 153 } 154 if (existing_value != null && existing_value.length() > 0) { 155 boolean found = selectValue((JComboBox) value_control, existing_value); 156 // It's possible that this is a custom value and so doesn't exist in the combobox 157 if (!found) { 158 // If so, add it then select it 159 ((JComboBox) value_control).addItem(existing_value); 160 ((JComboBox) value_control).setSelectedItem(existing_value); 161 } 162 } 163 else { 164 String default_value = argument.getDefaultValue(); 165 if (default_value != null) { 166 // if no namespace for default value, add ex. 167 // won't work if we want to set a non-metadata value 168 if (MetadataTools.getMetadataSetNamespace(default_value).equals("")) { 169 default_value = StaticStrings.EXTRACTED_NAMESPACE+default_value; 170 } 171 selectValue((JComboBox) value_control, default_value); 172 } 173 } 174 break; 175 176 case Argument.METADATA_SET_NAMESPACE: 177 value_control = new JComboBox(); 178 // !! Hack for exploding metadata databases: add the (empty) exploded metadata set 179 File exploded_metadata_set_file = new File(Gatherer.getGLIMetadataDirectoryPath() + "exp" + StaticStrings.METADATA_SET_EXTENSION); 180 MetadataSet exploded_metadata_set = new MetadataSet(exploded_metadata_set_file); 181 Gatherer.c_man.importMetadataSet(exploded_metadata_set); 182 183 // All the loaded metadata sets except the extracted metadata set are applicable 184 ArrayList metadata_sets = MetadataSetManager.getMetadataSets(); 185 for (int i = metadata_sets.size() - 1; i >= 0; i--) { 186 MetadataSet metadata_set = (MetadataSet) metadata_sets.get(i); 187 if (!(metadata_set.getNamespace().equals(MetadataSetManager.EXTRACTED_METADATA_NAMESPACE))) { 188 ((JComboBox)value_control).addItem(metadata_set); 189 } 190 } 191 192 selectValue((JComboBox) value_control, initial_value); 193 194 } // end of switch 195 196 // Enable or disable as necessary. 197 if(argument.isRequired() || argument.isAssigned() || is_enabled) { 198 if (enabled != null) { 199 enabled.setSelected(true); 200 } 201 if(value_control != null) { 202 value_control.setOpaque(true); 203 value_control.setBackground(Color.white); 204 value_control.setEnabled(true); 205 if(value_control instanceof JSpinner) { 206 // Set enabled 207 JComponent c = ((JSpinner)value_control).getEditor(); 208 if ( c instanceof JSpinner.DefaultEditor ) { 209 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c; 210 JFormattedTextField field = editor.getTextField(); 211 field.setEditable(true); 212 field.setBackground(Color.white); 213 } 214 } 215 } 216 } 217 else { 218 if (enabled != null) { 219 enabled.setSelected(false); 220 } 221 if(value_control != null) { 222 value_control.setOpaque(true); 223 value_control.setBackground(Color.lightGray); 224 value_control.setEnabled(false); 225 if(value_control instanceof JSpinner) { 226 // Set enabled 227 JComponent c = ((JSpinner)value_control).getEditor(); 228 if ( c instanceof JSpinner.DefaultEditor ) { 229 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c; 230 JFormattedTextField field = editor.getTextField(); 231 field.setEditable(false); 232 field.setBackground(Color.lightGray); 233 } 234 } 235 } 236 } 237 238 // Listener 239 if(value_control != null) { 240 if (argument.getType() != Argument.ENUM) { 241 // enums have already set tooltips based on option value 242 value_control.setToolTipText(tip); 243 } 244 add(value_control, BorderLayout.CENTER); 245 if (!argument.isRequired()) { 246 enabled.addActionListener(new EnabledListener(value_control)); 247 } 248 } 249 } 250 251 public Argument getArgument() { 252 return argument; 253 } 254 255 public String getArgumentName() { 256 return argument.getName(); 257 } 258 259 public String getValue() { 260 if(value_control == null) { 261 return null; 262 } 263 if (value_control instanceof JSpinner) { 264 return ((JSpinner)value_control).getValue().toString(); 265 } 266 if(value_control instanceof JComboBox) { 267 Object selected_item = ((JComboBox)value_control).getSelectedItem(); 268 if (selected_item != null) { 269 if (argument.getType() == Argument.METADATA_SET_NAMESPACE) { 270 return ((MetadataSet) selected_item).getNamespace(); 271 } 272 if (selected_item instanceof Argument.ArgumentOption) { 273 return ((Argument.ArgumentOption)selected_item).name; 274 } 275 if (selected_item instanceof MetadataElement) { 276 return ((MetadataElement) selected_item).getFullName(); 277 } 278 return selected_item.toString(); 279 } 280 return null; 281 } 282 if(value_control instanceof JTextField) { 283 return ((JTextField)value_control).getText(); 284 } 285 return null; 286 } 287 /** Retrieve the control used for storing values. 288 * @return JComponent 289 */ 290 public JComponent getValueControl() { 291 return value_control; 292 } 293 294 public boolean isEnabled() { 295 if (enabled == null) { 296 return true; // always enabled 297 } 298 return enabled.isSelected(); 299 } 300 301 302 /** Identifies this control by returning the name of the Argument it is based on. 303 * @return The name of the Argument as a <strong>String</strong>. 304 * @see org.greenstone.gatherer.cdm.Argument 305 */ 306 public String toString() { 307 return argument.getName(); 308 } 309 /** Updates the enwrapped Argument using the values provided by the controls. 310 * @return <i>true</i> if the update was successful, <i>false</i> otherwise. 311 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.Argument.ArgumentOption 312 * @see org.greenstone.gatherer.cdm.Language 313 */ 314 public boolean updateArgument() { 315 if(argument.isRequired() || enabled.isSelected() ) { 316 argument.setAssigned(false); 317 String result = null; 318 switch(argument.getType()) { 319 case Argument.ENUM: 320 Argument.ArgumentOption option = (Argument.ArgumentOption)((JComboBox)value_control).getSelectedItem(); 321 if(option != null && option.name.length() > 0) { 322 argument.setValue(option.name); 323 } 324 else { 325 String args[] = new String[1]; 326 args[0] = argument.getName(); 327 if(argument.isRequired()) { 328 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); 329 } 330 // They've left the field blank 331 else { 332 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); 333 argument.setValue(null); 334 } 335 args = null; 336 return false; 337 } 338 argument.setAssigned(true); 339 return true; 340 case Argument.FLAG: 341 // Should have already been handled above. 342 argument.setAssigned(true); 343 return true; 344 case Argument.INTEGER: 345 result = ((JSpinner)value_control).getValue().toString(); 346 if(result.length() > 0) { 347 // Test if the value entered is a valid int. 348 try { 349 int x = Integer.parseInt(result); 350 } 351 catch(NumberFormatException nfe) { 352 String args[] = new String[2]; 353 args[0] = argument.getName(); 354 args[1] = result; 355 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Bad_Integer", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); 356 args = null; 357 return false; 358 } 359 argument.setValue(result); 360 } 361 else { 362 String args[] = new String[1]; 363 args[0] = argument.getName(); 364 if(argument.isRequired()) { 365 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); 366 } 367 // They've left the field blank 368 else { 369 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); 370 argument.setValue(null); 371 } 372 args = null; 373 return false; 374 } 375 argument.setAssigned(true); 376 return true; 377 case Argument.LANGUAGE: 378 String language = (((JComboBox)value_control).getSelectedItem()).toString(); 379 argument.setValue(language); 380 // Kinda lucked out here. Its impossible not to choose an entry from these comboboxes as they are restricted. 381 argument.setAssigned(true); 382 return true; 383 case Argument.METADATUM: 384 case Argument.METADATA: 385 Object new_value_raw = ((JComboBox) value_control).getSelectedItem(); 386 if (new_value_raw instanceof MetadataElement) { 387 argument.setValue(((MetadataElement) new_value_raw).getFullName()); 388 } 389 else { 390 // But we have to be careful as an arbitary string object could be zero length 391 String new_value = new_value_raw.toString(); 392 ///ystem.err.println("The current value is: " + new_value); 393 if(new_value.length() > 0) { 394 argument.setValue(new_value); 395 } 396 else { 397 String args[] = new String[1]; 398 args[0] = argument.getName(); 399 if(argument.isRequired()) { 400 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); 401 } 402 // They've left the field blank 403 else { 404 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); 405 argument.setValue(null); 406 } 407 args = null; 408 return false; 409 } 410 } 411 argument.setAssigned(true); 412 return true; 413 case Argument.REGEXP: 414 case Argument.STRING: 415 result = ((JTextField)value_control).getText(); 416 if(result.length() > 0) { 417 argument.setValue(result); 418 } 419 else { 420 String args[] = new String[1]; 421 args[0] = argument.getName(); 422 if(argument.isRequired()) { 423 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); 424 } 425 // They've left the field blank 426 else { 427 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); 428 argument.setValue(null); 429 } 430 args = null; 431 return false; 432 } 433 argument.setAssigned(true); 434 return true; 435 } 436 return false; 437 } 438 else { 439 argument.setAssigned(false); 440 return true; 441 } 442 } 443 444 445 public boolean updateArgument(boolean checkRequired) { 446 447 448 if (checkRequired){ 449 return updateArgument(); 450 } 451 else{ 452 if (argument.getType() == Argument.STRING){ 453 String result = ((JTextField)value_control).getText(); 454 455 if(result.length() > 0) { 456 argument.setValue(result); 457 argument.setAssigned(true); 458 } 459 } 460 } 461 462 return true; 463 464 } 465 466 467 468 /** Method to ensure that a certain value is selected, if it exists within that combobox to begin with. 469 * @param combobox The <strong>JComboBox</strong> whose selection we are trying to preset. 470 * @param target The desired value of the selection as a <strong>String</strong>. 471 * @return true if the item was found and selected, false otherwise 472 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.Argument.ArgumentOption 473 */ 474 public static boolean selectValue(JComboBox combobox, String target) 475 { 476 for (int i = 0; i < combobox.getItemCount(); i++) { 477 Object object = combobox.getItemAt(i); 478 479 if (object instanceof Argument.ArgumentOption) { 480 Argument.ArgumentOption opt = (Argument.ArgumentOption) object; 481 if (opt.name.startsWith(target)) { 482 combobox.setSelectedIndex(i); 483 combobox.setToolTipText(opt.getToolTip()); 484 return true; 485 } 486 } 487 else if (object instanceof MetadataElement) { 488 if(((MetadataElement)object).getFullName().equals(target)) { 489 combobox.setSelectedIndex(i); 490 return true; 491 } 492 } 493 else if (object.toString().equals(target)) { 494 combobox.setSelectedIndex(i); 495 return true; 496 } 497 } 498 499 return false; 500 } 501 502 503 /** Forces the control into an 'enabled' mode. */ 504 public void setEnabled() { 188 505 enabled.setSelected(true); 189 } 190 if(value_control != null) { 191 value_control.setOpaque(true); 192 value_control.setBackground(Color.white); 193 value_control.setEnabled(true); 194 if(value_control instanceof JSpinner) { 195 // Set enabled 196 JComponent c = ((JSpinner)value_control).getEditor(); 197 if ( c instanceof JSpinner.DefaultEditor ) { 198 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c; 199 JFormattedTextField field = editor.getTextField(); 200 field.setEditable(true); 201 field.setBackground(Color.white); 202 } 203 } 204 } 205 } 206 else { 207 if (enabled != null) { 208 enabled.setSelected(false); 209 } 210 if(value_control != null) { 211 value_control.setOpaque(true); 212 value_control.setBackground(Color.lightGray); 213 value_control.setEnabled(false); 214 if(value_control instanceof JSpinner) { 215 // Set enabled 216 JComponent c = ((JSpinner)value_control).getEditor(); 217 if ( c instanceof JSpinner.DefaultEditor ) { 218 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c; 219 JFormattedTextField field = editor.getTextField(); 220 field.setEditable(false); 221 field.setBackground(Color.lightGray); 222 } 223 } 224 } 225 } 226 227 // Listener 228 if(value_control != null) { 229 if (argument.getType() != Argument.ENUM) { 230 // enums have already set tooltips based on option value 231 value_control.setToolTipText(tip); 232 } 233 add(value_control, BorderLayout.CENTER); 234 if (!argument.isRequired()) { 235 enabled.addActionListener(new EnabledListener(value_control)); 236 } 237 } 238 } 239 240 public Argument getArgument() { 241 return argument; 242 } 243 244 public String getArgumentName() { 245 return argument.getName(); 246 } 247 248 public String getValue() { 249 if(value_control == null) { 250 return null; 251 } 252 if (value_control instanceof JSpinner) { 253 return ((JSpinner)value_control).getValue().toString(); 254 } 255 if(value_control instanceof JComboBox) { 256 Object selected_item = ((JComboBox)value_control).getSelectedItem(); 257 if (selected_item != null) { 258 if (argument.getType() == Argument.METADATA_SET_NAMESPACE) { 259 return ((MetadataSet) selected_item).getNamespace(); 260 } 261 if (selected_item instanceof Argument.ArgumentOption) { 262 return ((Argument.ArgumentOption)selected_item).name; 263 } 264 if (selected_item instanceof MetadataElement) { 265 return ((MetadataElement) selected_item).getFullName(); 266 } 267 return selected_item.toString(); 268 } 269 return null; 270 } 271 if(value_control instanceof JTextField) { 272 return ((JTextField)value_control).getText(); 273 } 274 return null; 275 } 276 /** Retrieve the control used for storing values. 277 * @return JComponent 278 */ 279 public JComponent getValueControl() { 280 return value_control; 281 } 282 283 public boolean isEnabled() { 284 if (enabled == null) { 285 return true; // always enabled 286 } 287 return enabled.isSelected(); 288 } 289 290 291 /** Identifies this control by returning the name of the Argument it is based on. 292 * @return The name of the Argument as a <strong>String</strong>. 293 * @see org.greenstone.gatherer.cdm.Argument 294 */ 295 public String toString() { 296 return argument.getName(); 297 } 298 /** Updates the enwrapped Argument using the values provided by the controls. 299 * @return <i>true</i> if the update was successful, <i>false</i> otherwise. 300 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.Argument.ArgumentOption 301 * @see org.greenstone.gatherer.cdm.Language 302 */ 303 public boolean updateArgument() { 304 if(argument.isRequired() || enabled.isSelected() ) { 305 argument.setAssigned(false); 306 String result = null; 307 switch(argument.getType()) { 308 case Argument.ENUM: 309 Argument.ArgumentOption option = (Argument.ArgumentOption)((JComboBox)value_control).getSelectedItem(); 310 if(option != null && option.name.length() > 0) { 311 argument.setValue(option.name); 312 } 313 else { 314 String args[] = new String[1]; 315 args[0] = argument.getName(); 316 if(argument.isRequired()) { 317 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); 318 } 319 // They've left the field blank 320 else { 321 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); 322 argument.setValue(null); 323 } 324 args = null; 325 return false; 326 } 327 argument.setAssigned(true); 328 return true; 329 case Argument.FLAG: 330 // Should have already been handled above. 331 argument.setAssigned(true); 332 return true; 333 case Argument.INTEGER: 334 result = ((JSpinner)value_control).getValue().toString(); 335 if(result.length() > 0) { 336 // Test if the value entered is a valid int. 337 try { 338 int x = Integer.parseInt(result); 339 } 340 catch(NumberFormatException nfe) { 341 String args[] = new String[2]; 342 args[0] = argument.getName(); 343 args[1] = result; 344 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Bad_Integer", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); 345 args = null; 346 return false; 347 } 348 argument.setValue(result); 349 } 350 else { 351 String args[] = new String[1]; 352 args[0] = argument.getName(); 353 if(argument.isRequired()) { 354 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); 355 } 356 // They've left the field blank 357 else { 358 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); 359 argument.setValue(null); 360 } 361 args = null; 362 return false; 363 } 364 argument.setAssigned(true); 365 return true; 366 case Argument.LANGUAGE: 367 String language = (((JComboBox)value_control).getSelectedItem()).toString(); 368 argument.setValue(language); 369 // Kinda lucked out here. Its impossible not to choose an entry from these comboboxes as they are restricted. 370 argument.setAssigned(true); 371 return true; 372 case Argument.METADATUM: 373 case Argument.METADATA: 374 Object new_value_raw = ((JComboBox) value_control).getSelectedItem(); 375 if (new_value_raw instanceof MetadataElement) { 376 argument.setValue(((MetadataElement) new_value_raw).getFullName()); 377 } 378 else { 379 // But we have to be careful as an arbitary string object could be zero length 380 String new_value = new_value_raw.toString(); 381 ///ystem.err.println("The current value is: " + new_value); 382 if(new_value.length() > 0) { 383 argument.setValue(new_value); 384 } 385 else { 386 String args[] = new String[1]; 387 args[0] = argument.getName(); 388 if(argument.isRequired()) { 389 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); 390 } 391 // They've left the field blank 506 } 507 /** Explicitly sets the value of a JTextField type control to the given String. 508 * @param value_str The new value of the control as a <strong>String</strong>. 509 */ 510 public void setValue(String value_str) { 511 ((JTextField)value_control).setText(value_str); 512 } 513 514 /** Listens for actions apon the enable checkbox, and if detected enables or diables control appropriately. */ 515 private class EnabledListener 516 implements ActionListener { 517 /** An editor component, such as a JComboBox or JTextField, that might have its enabled state changed by this listener. */ 518 private JComponent target = null; 519 520 /** Constructor. */ 521 public EnabledListener(JComponent target) { 522 this.target = target; 523 } 524 525 /** Any implementation of ActionListener must include this method so that we can be informed when an action has been performed on or registered check box, prompting us to change the state of the other controls as per the users request. 526 * @param event An <strong>ActionEvent</strong> containing information about the click. 527 */ 528 public void actionPerformed(ActionEvent event) { 529 if (this.target == null) { 530 return; 531 } 532 JCheckBox source = (JCheckBox)event.getSource(); 533 if(source.isSelected()) { 534 target.setBackground(Color.white); 535 target.setEnabled(true); 536 } 392 537 else { 393 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); 394 argument.setValue(null); 395 } 396 args = null; 397 return false; 398 } 399 } 400 argument.setAssigned(true); 401 return true; 402 case Argument.REGEXP: 403 case Argument.STRING: 404 result = ((JTextField)value_control).getText(); 405 if(result.length() > 0) { 406 argument.setValue(result); 407 } 408 else { 409 String args[] = new String[1]; 410 args[0] = argument.getName(); 411 if(argument.isRequired()) { 412 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); 413 } 414 // They've left the field blank 415 else { 416 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); 417 argument.setValue(null); 418 } 419 args = null; 420 return false; 421 } 422 argument.setAssigned(true); 423 return true; 424 } 425 return false; 426 } 427 else { 428 argument.setAssigned(false); 429 return true; 430 } 431 } 432 433 434 public boolean updateArgument(boolean checkRequired) { 435 436 437 if (checkRequired){ 438 return updateArgument(); 439 } 440 else{ 441 if (argument.getType() == Argument.STRING){ 442 String result = ((JTextField)value_control).getText(); 443 444 if(result.length() > 0) { 445 argument.setValue(result); 446 argument.setAssigned(true); 447 } 448 } 449 } 450 451 return true; 452 453 } 454 455 456 457 /** Method to ensure that a certain value is selected, if it exists within that combobox to begin with. 458 * @param combobox The <strong>JComboBox</strong> whose selection we are trying to preset. 459 * @param target The desired value of the selection as a <strong>String</strong>. 460 * @return true if the item was found and selected, false otherwise 461 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.Argument.ArgumentOption 462 */ 463 public static boolean selectValue(JComboBox combobox, String target) 464 { 465 for (int i = 0; i < combobox.getItemCount(); i++) { 466 Object object = combobox.getItemAt(i); 467 468 if (object instanceof Argument.ArgumentOption) { 469 Argument.ArgumentOption opt = (Argument.ArgumentOption) object; 470 if (opt.name.startsWith(target)) { 471 combobox.setSelectedIndex(i); 472 combobox.setToolTipText(opt.getToolTip()); 473 return true; 474 } 475 } 476 else if (object instanceof MetadataElement) { 477 if(((MetadataElement)object).getFullName().equals(target)) { 478 combobox.setSelectedIndex(i); 479 return true; 480 } 481 } 482 else if (object.toString().equals(target)) { 483 combobox.setSelectedIndex(i); 484 return true; 485 } 486 } 487 488 return false; 489 } 490 491 492 /** Forces the control into an 'enabled' mode. */ 493 public void setEnabled() { 494 enabled.setSelected(true); 495 } 496 /** Explicitly sets the value of a JTextField type control to the given String. 497 * @param value_str The new value of the control as a <strong>String</strong>. 498 */ 499 public void setValue(String value_str) { 500 ((JTextField)value_control).setText(value_str); 501 } 502 503 /** Listens for actions apon the enable checkbox, and if detected enables or diables control appropriately. */ 504 private class EnabledListener 538 target.setBackground(Color.lightGray); 539 target.setEnabled(false); 540 } 541 // Special case of stupid JSpinners who don't let their backgrounds change properly. 542 if(target instanceof JSpinner) { 543 JSpinner spinner = (JSpinner) target; 544 JComponent c = spinner.getEditor(); 545 if ( c instanceof JSpinner.DefaultEditor ) { 546 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c; 547 JFormattedTextField field = editor.getTextField(); 548 field.setEditable(source.isSelected()); 549 if(source.isSelected()) { 550 field.setBackground(Color.white); 551 } 552 else { 553 field.setBackground(Color.lightGray); 554 } 555 } 556 } 557 } 558 } 559 560 561 /** Listener that sets the tooltip associated to a combobox to the tooltip relevant to the selected item. */ 562 private class ToolTipUpdater 505 563 implements ActionListener { 506 /** An editor component, such as a JComboBox or JTextField, that might have its enabled state changed by this listener. */ 507 private JComponent target = null; 508 509 /** Constructor. */ 510 public EnabledListener(JComponent target) { 511 this.target = target; 512 } 513 514 /** Any implementation of ActionListener must include this method so that we can be informed when an action has been performed on or registered check box, prompting us to change the state of the other controls as per the users request. 515 * @param event An <strong>ActionEvent</strong> containing information about the click. 516 */ 517 public void actionPerformed(ActionEvent event) { 518 if (this.target == null) { 519 return; 520 } 521 JCheckBox source = (JCheckBox)event.getSource(); 522 if(source.isSelected()) { 523 target.setBackground(Color.white); 524 target.setEnabled(true); 525 } 526 else { 527 target.setBackground(Color.lightGray); 528 target.setEnabled(false); 529 } 530 // Special case of stupid JSpinners who don't let their backgrounds change properly. 531 if(target instanceof JSpinner) { 532 JSpinner spinner = (JSpinner) target; 533 JComponent c = spinner.getEditor(); 534 if ( c instanceof JSpinner.DefaultEditor ) { 535 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c; 536 JFormattedTextField field = editor.getTextField(); 537 field.setEditable(source.isSelected()); 538 if(source.isSelected()) { 539 field.setBackground(Color.white); 540 } 541 else { 542 field.setBackground(Color.lightGray); 543 } 544 } 545 } 546 } 547 } 548 549 550 /** Listener that sets the tooltip associated to a combobox to the tooltip relevant to the selected item. */ 551 private class ToolTipUpdater 552 implements ActionListener { 553 /** Any implementation of an ActionListener must include this method so that we can be informed when the selection in a combobox has changed and update the tooltip accordingly. 554 * @param event An <strong>ActionEvent</strong> containing information about the action that fired this call. 555 */ 556 public void actionPerformed(ActionEvent event) { 557 JComboBox source = (JComboBox)event.getSource(); 558 Object object = source.getSelectedItem(); 559 if(object instanceof Argument.ArgumentOption) { 560 Argument.ArgumentOption opt = (Argument.ArgumentOption)object; 561 if(opt != null) { 562 source.setToolTipText(opt.getToolTip()); 563 } 564 else { 565 source.setToolTipText(StaticStrings.EMPTY_STR); 566 } 567 } 568 } 569 } 564 /** Any implementation of an ActionListener must include this method so that we can be informed when the selection in a combobox has changed and update the tooltip accordingly. 565 * @param event An <strong>ActionEvent</strong> containing information about the action that fired this call. 566 */ 567 public void actionPerformed(ActionEvent event) { 568 JComboBox source = (JComboBox)event.getSource(); 569 Object object = source.getSelectedItem(); 570 if(object instanceof Argument.ArgumentOption) { 571 Argument.ArgumentOption opt = (Argument.ArgumentOption)object; 572 if(opt != null) { 573 source.setToolTipText(opt.getToolTip()); 574 } 575 else { 576 source.setToolTipText(StaticStrings.EMPTY_STR); 577 } 578 } 579 } 580 } 570 581 }
Note:
See TracChangeset
for help on using the changeset viewer.