alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

// $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/testbeans/gui/ComboStringEditor.java,v 1.3 2004/02/10 21:24:01 jsalvata Exp $
/*
 * Copyright 2003-2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jmeter.testbeans.gui;

import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyEditorSupport;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.text.JTextComponent;

import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

/**
 * This class implements a property editor for possibly null String properties
 * that supports custom editing (i.e.: provides a GUI component) based
 * on a combo box.
 * 

* The provided GUI is a combo box with: *

    *
  • An option for "undefined" (corresponding to the null value), unless * the noUndefined property is set. *
  • An option for each value in the tags property. *
  • The possibility to write your own value, unless the noEdit * property is set. *
* * @author Jordi Salvat i Alabart * @version $Revision: 1.3 $ updated on $Date: 2004/02/10 21:24:01 $ */ class ComboStringEditor extends PropertyEditorSupport implements ItemListener { protected static Logger log= LoggingManager.getLoggerForClass(); /** * The list of options to be offered by this editor. */ private String[] tags= new String[0]; /** * True iif the editor should not accept (nor produce) a null value. */ private boolean noUndefined= false; /** * True iif the editor should not accept (nor produce) any non-null * values different from the provided tags. */ private boolean noEdit= false; /** * The edited property's default value. */ private String initialEditValue; private JComboBox combo; private DefaultComboBoxModel model; private boolean startingEdit= false; /* True iif we're currently processing an event triggered by the user * selecting the "Edit" option. Used to prevent reverting the combo * to non-editable during processing of secondary events. */ private static final Object UNDEFINED= new UniqueObject(JMeterUtils.getResString("property_undefined")); private static final Object EDIT= new UniqueObject(JMeterUtils.getResString("property_edit")); ComboStringEditor() { // Create the combo box we will use to edit this property: model= new DefaultComboBoxModel(); model.addElement(UNDEFINED); model.addElement(EDIT); combo= new JComboBox(model); combo.addItemListener(this); combo.setEditable(false); } /* (non-Javadoc) * @see java.beans.PropertyEditor#supportsCustomEditor() */ public boolean supportsCustomEditor() { return true; } /* (non-Javadoc) * @see java.beans.PropertyEditor#getCustomEditor() */ public Component getCustomEditor() { return combo; } /* (non-Javadoc) * @see java.beans.PropertyEditor#getValue() */ public Object getValue() { return getAsText(); } /* (non-Javadoc) * @see java.beans.PropertyEditor#getAsText() */ public String getAsText() { Object value= combo.getSelectedItem(); if (value == UNDEFINED) return null; else return (String)value; } /* (non-Javadoc) * @see java.beans.PropertyEditor#setValue() */ public void setValue(Object value) { setAsText((String)value); } /* (non-Javadoc) * @see java.beans.PropertyEditor#setAsText() */ public void setAsText(String value) { combo.setEditable(true); if (value == null) combo.setSelectedItem(UNDEFINED); else combo.setSelectedItem(value); if (! startingEdit && combo.getSelectedIndex() >= 0) { combo.setEditable(false); } } /* (non-Javadoc) * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent) */ public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { if (e.getItem() == EDIT) { startingEdit= true; startEditing(); startingEdit= false; } else { if (!startingEdit && combo.getSelectedIndex() >= 0) { combo.setEditable(false); } firePropertyChange(); } } } private void startEditing() { JTextComponent textField= (JTextComponent)combo.getEditor().getEditorComponent(); combo.setEditable(true); textField.requestFocus(); String text= initialEditValue; if (initialEditValue == null) text= ""; // will revert to last valid value if invalid combo.setSelectedItem(text); int i= text.indexOf("${}"); if (i != -1) textField.setCaretPosition(i+2); else textField.selectAll(); } /** * @return */ public String getInitialEditValue() { return initialEditValue; } /** * @return */ public boolean getNoEdit() { return noEdit; } /** * @return */ public boolean getNoUndefined() { return noUndefined; } /** * @return */ public String[] getTags() { return tags; } /** * @param object */ public void setInitialEditValue(String object) { initialEditValue= object; } /** * @param b */ public void setNoEdit(boolean b) { if (noEdit == b) return; noEdit= b; if (noEdit) model.removeElement(EDIT); else model.addElement(EDIT); } /** * @param b */ public void setNoUndefined(boolean b) { if (noUndefined == b) return; noUndefined= b; if (noUndefined) model.removeElement(UNDEFINED); else model.insertElementAt(UNDEFINED, 0); } /** * @param strings */ public void setTags(String[] strings) { if (tags.equals(strings)) return; for (int i=0; i
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.