Save This Page
Home » iText-src-2.1.3 » com.lowagie » tools » arguments » [javadoc | source]
    1   /*
    2    * $Id: OptionArgument.java,v 1.6 2005/03/31 07:23:03 blowagie Exp $
    3    * $Name:  $
    4    *
    5    * Copyright 2005 by Bruno Lowagie.
    6    *
    7    * The contents of this file are subject to the Mozilla Public License Version 1.1
    8    * (the "License"); you may not use this file except in compliance with the License.
    9    * You may obtain a copy of the License at http://www.mozilla.org/MPL/
   10    *
   11    * Software distributed under the License is distributed on an "AS IS" basis,
   12    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
   13    * for the specific language governing rights and limitations under the License.
   14    *
   15    * The Original Code is 'iText, a free JAVA-PDF library'.
   16    *
   17    * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
   18    * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
   19    * All Rights Reserved.
   20    * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
   21    * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
   22    *
   23    * Contributor(s): all the names of the contributors are added in the source code
   24    * where applicable.
   25    *
   26    * Alternatively, the contents of this file may be used under the terms of the
   27    * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
   28    * provisions of LGPL are applicable instead of those above.  If you wish to
   29    * allow use of your version of this file only under the terms of the LGPL
   30    * License and not to allow others to use your version of this file under
   31    * the MPL, indicate your decision by deleting the provisions above and
   32    * replace them with the notice and other provisions required by the LGPL.
   33    * If you do not delete the provisions above, a recipient may use your version
   34    * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
   35    *
   36    * This library is free software; you can redistribute it and/or modify it
   37    * under the terms of the MPL as stated above or under the terms of the GNU
   38    * Library General Public License as published by the Free Software Foundation;
   39    * either version 2 of the License, or any later version.
   40    *
   41    * This library is distributed in the hope that it will be useful, but WITHOUT
   42    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   43    * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
   44    * details.
   45    *
   46    * If you didn't download this code from the following link, you should check if
   47    * you aren't using an obsolete version:
   48    * http://www.lowagie.com/iText/
   49    */
   50   package com.lowagie.tools.arguments;
   51   
   52   import java.awt.event.ActionEvent;
   53   import java.util.Iterator;
   54   import java.util.TreeMap;
   55   
   56   import javax.swing.JComboBox;
   57   import javax.swing.JOptionPane;
   58   
   59   import com.lowagie.tools.plugins.AbstractTool;
   60   
   61   /**
   62    * Argument that can be one of several options.
   63    */
   64   public class OptionArgument extends ToolArgument {
   65   
   66   	/**
   67   	 * An Entry that can be chosen as option.
   68   	 */
   69   	public class Entry {
   70   		/** Describes the option. */
   71   		private Object description;
   72   		/** Holds the actual value of the option. */
   73   		private Object value;
   74   		/**
   75   		 * Constructs an entry. 
   76   		 * @param value the value of the entry (that wil be identical to the description)
   77   		 */
   78   		public Entry(Object value) {
   79   			this.value = value;
   80   			this.description = value;
   81   		}
   82   		/**
   83   		 * Constructs an entry. 
   84   		 * @param description the description of the entry
   85   		 * @param value the value of the entry
   86   		 */
   87   		public Entry(Object description, Object value) {
   88   			this.description = description;
   89   			this.value = value;
   90   		}
   91   		/**
   92   		 * String representation of the Entry.
   93   		 * @return a description of the entry
   94   		 */
   95   		public String toString() {
   96   			return description.toString();
   97   		}
   98   		/**
   99   		 * Gets the value of the String.
  100   		 * @return the toString of the value
  101   		 */
  102   		public String getValueToString() {
  103   			return value.toString();
  104   		}
  105   		/**
  106   		 * @return Returns the description.
  107   		 */
  108   		public Object getDescription() {
  109   			return description;
  110   		}
  111   		/**
  112   		 * @param description The description to set.
  113   		 */
  114   		public void setDescription(Object description) {
  115   			this.description = description;
  116   		}
  117   		/**
  118   		 * @return Returns the value.
  119   		 */
  120   		public Object getValue() {
  121   			return value;
  122   		}
  123   		/**
  124   		 * @param value The value to set.
  125   		 */
  126   		public void setValue(Object value) {
  127   			this.value = value;
  128   		}
  129   	}
  130   	
  131   	private TreeMap options = new TreeMap();
  132   	
  133   	/**
  134   	 * Constructs an OptionArgument.
  135   	 * @param tool the tool that needs this argument
  136   	 * @param name the name of the argument
  137   	 * @param description the description of the argument
  138   	 */
  139   	public OptionArgument(AbstractTool tool, String name, String description) {
  140   		super(tool, name, description, Entry.class.getName());
  141   	}
  142   	
  143   	/**
  144   	 * Adds an Option.
  145   	 * @param description the description of the option
  146   	 * @param value the value of the option
  147   	 */
  148   	public void addOption(Object description, Object value) {
  149   		options.put(value.toString(), new Entry(description, value));
  150   	}
  151   	
  152   	/**
  153   	 * Gets the argument as an object.
  154   	 * @return an object
  155   	 * @throws InstantiationException
  156   	 */
  157   	public Object getArgument() throws InstantiationException {
  158   		if (value == null) return null;
  159   		try {
  160   			return ((Entry)options.get(value)).getValue();
  161   		} catch (Exception e) {
  162   			throw new InstantiationException(e.getMessage());
  163   		}
  164   	}
  165   	
  166   	/**
  167   	 * @see com.lowagie.tools.arguments.ToolArgument#getUsage()
  168   	 */
  169   	public String getUsage() {
  170   		StringBuffer buf = new StringBuffer(super.getUsage());
  171   		buf.append("    possible options:\n");
  172   		Entry entry;
  173   		for (Iterator i = options.values().iterator(); i.hasNext(); ) {
  174   			entry = (Entry)i.next();
  175   			buf.append("    - ");
  176   			buf.append(entry.getValueToString());
  177   			buf.append(": ");
  178   			buf.append(entry.toString());
  179   			buf.append("\n");
  180   		}
  181   		return buf.toString();
  182   	}
  183   	
  184   	/**
  185   	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
  186   	 */
  187   	public void actionPerformed(ActionEvent evt) {
  188   		Object[] message = new Object[2];
  189   		message[0] = "Choose one of the following options:";
  190   		JComboBox cb = new JComboBox();
  191   		for(Iterator i = options.values().iterator(); i.hasNext(); ) {
  192   			cb.addItem(i.next());
  193   		}
  194   		message[1] = cb;
  195   		int result = JOptionPane.showOptionDialog( 
  196   	 		    tool.getInternalFrame(),
  197   	 		    message, 
  198   	 		    description,
  199   	 		    JOptionPane.OK_CANCEL_OPTION, 
  200   	 		    JOptionPane.QUESTION_MESSAGE,
  201   	 		    null,
  202   	 		    null,
  203   				null
  204   	 		);
  205   		if (result == 0) {
  206   			Entry entry = (Entry)cb.getSelectedItem();
  207   			setValue(entry.getValueToString());
  208   		}
  209   	}
  210   }

Save This Page
Home » iText-src-2.1.3 » com.lowagie » tools » arguments » [javadoc | source]