|
Re: Combos in menu [message #456747 is a reply to message #456726] |
Wed, 08 June 2005 12:55 |
Mark Diggory Messages: 202 Registered: July 2009 |
Senior Member |
|
|
This is a multi-part message in MIME format.
--------------030004030202080206060104
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
I'm new to the swt news list, but I think you want to use a
"ControlContribution", I've done this in the attached AddressEntry
dropdown contribution to my 'Browser View'. I hooked it into the
BrowserView like so.
final AddressEntry address = new AddressEntry("Address");
toolBarManager.add(address);
The one difficulty I have is in getting the length to behave the way I'd
like, I wanted it to grab all the horizontal space in the tool bar, but
this wasn't possible. If you ever come across a solution to this I'd be
grateful if you sent it my way.
Cheers,
Mark Diggory
Jed wrote:
> Hi,
> can anyone help with adding combo widgets in a menu for a view? Similar
> to drop down menus you get in Word for selecting fonts. Thanks.
>
--------------030004030202080206060104
Content-Type: text/x-java-source;
name="AddressEntry.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="AddressEntry.java"
/*********************************************************** ********************
* Copyright (c) 2000, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
************************************************************ *******************/
package org.worldbank.toolkit.cdrom.browser;
import java.util.Iterator;
import java.util.TreeSet;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.action.ControlContribution;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
/**
* The helper class for creating entry fields with label and text. Optionally,
* a button can be added after the text. The attached listener reacts to all
* the events. Entring new text makes the entry 'dirty', but only when 'commit'
* is called is 'valueChanged' method called (and only if 'dirty' flag is set).
* This allows delayed commit.
*/
public class AddressEntry extends ControlContribution{
private TreeSet history = new TreeSet();
private Combo text;
private String value = "";
private boolean dirty;
boolean ignoreModify = false;
private IAddressEntryListener listener;
/**
* @param id
*/
protected AddressEntry(String id) {
super(id);
}
/* (non-Javadoc)
* @see org.eclipse.jface.action.ControlContribution#createControl(o rg.eclipse.swt.widgets.Composite)
*/
protected Control createControl(Composite parent) {
text = new Combo(parent, SWT.DROP_DOWN|SWT.FLAT); //$NON-NLS-1$
text.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
keyReleaseOccured(e);
}
});
text.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
editOccured(e);
}
});
return text;
}
protected int computeWidth(Control control) {
System.out.println(control.getLayoutData());
return control.computeSize(350, SWT.DEFAULT, true).x;
}
/**
* Attaches the listener for the entry.
*
* @param listener
*/
public void setFormEntryListener(IAddressEntryListener listener) {
this.listener = listener;
}
/**
* If dirty, commits the text in the widget to the value and notifies the
* listener. This call clears the 'dirty' flag.
*
*/
public void commit() {
if (dirty) {
if (text.getText().length()>0){
value = text.getText();
addToHistory(value);
if (listener != null)
listener.addressChanged(this);
}
}
dirty = false;
}
public void cancelEdit() {
dirty = false;
}
private void editOccured(ModifyEvent e) {
if (ignoreModify)
return;
dirty = true;
}
/**
* Returns the text control.
*
* @return
*/
public Combo getAddress() {
return text;
}
/**
* Returns the current entry value. If the entry is dirty and was not
* commited, the value may be different from the text in the widget.
*
* @return
*/
public String getValue() {
return value;
}
/**
* Returns true if the text has been modified.
*
* @return
*/
public boolean isDirty() {
return dirty;
}
private void keyReleaseOccured(KeyEvent e) {
if (e.character == '\r') {
// commit value
if (dirty)
commit();
} else if (e.character == '\u001b') { // Escape character
text.setText(value != null ? value : ""); // restore old //$NON-NLS-1$
dirty = false;
}
}
/**
* Sets the value of this entry.
*
* @param value
*/
public void setValue(String value) {
if (text != null && value != null && !"".equals(value)){
this.value = value;
addToHistory(value);
}
}
private void addToHistory(String value){
if (value != null && !"".equals(value)){
history.add(value);
}
reloadHistory();
text.setText(value);
}
private void reloadHistory(){
String[] items = new String[history.size()];
Iterator iter = history.iterator();
for(int i = 0 ; i < items.length ; i++){
items[i] = iter.next().toString();
}
text.setItems(items);
}
/**
* Sets the value of this entry with the possibility to turn the
* notification off.
*
* @param value
* @param blockNotification
*/
public void setValue(String value, boolean blockNotification) {
ignoreModify = blockNotification;
setValue(value);
ignoreModify = false;
}
}
--------------030004030202080206060104--
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03330 seconds