Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Combos in menu
Combos in menu [message #456726] Tue, 07 June 2005 18:30 Go to next message
Jed is currently offline JedFriend
Messages: 4
Registered: July 2009
Junior Member
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.
Re: Combos in menu [message #456747 is a reply to message #456726] Wed, 08 June 2005 12:55 Go to previous messageGo to next message
Mark Diggory is currently offline Mark DiggoryFriend
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--
Re: Combos in menu [message #456820 is a reply to message #456747] Thu, 09 June 2005 19:19 Go to previous messageGo to next message
Jed is currently offline JedFriend
Messages: 4
Registered: July 2009
Junior Member
Hey thanks for the reply. I'm still a bit confused. What's an
AddressEntry? Which method did you put that code in? Where does combo
widget come into place? Thanks.
Re: Combos in menu [message #456821 is a reply to message #456747] Thu, 09 June 2005 19:26 Go to previous messageGo to next message
Jed is currently offline JedFriend
Messages: 4
Registered: July 2009
Junior Member
Hi,

lol, nvm me..didnt see the attached file.
Re: Combos in menu [message #456822 is a reply to message #456747] Thu, 09 June 2005 19:51 Go to previous messageGo to next message
Jed is currently offline JedFriend
Messages: 4
Registered: July 2009
Junior Member
Hey Mark,

In order to change the length of the widget, just modify the default method
computeWidth in the AddressEntry. Its length at default is 350.

protected int computeWidth(Control control) {

System.out.println(control.getLayoutData());
return control.computeSize(350, SWT.DEFAULT, true).x;
}
Re: Combos in menu [message #456828 is a reply to message #456822] Thu, 09 June 2005 23:14 Go to previous message
Mark Diggory is currently offline Mark DiggoryFriend
Messages: 202
Registered: July 2009
Senior Member
The problem is that its not a very dynamic situation. I don't see how
(like in a layout) to just designate that it grab/fill the available
space of the toolbar, which is the behavior I'm seeking.

Its not a big issue for now, Thanks.
-Mark

Jed wrote:
> Hey Mark,
> In order to change the length of the widget, just modify the default method
> computeWidth in the AddressEntry. Its length at default is 350.
> protected int computeWidth(Control control) {
>
> System.out.println(control.getLayoutData());
> return control.computeSize(350, SWT.DEFAULT, true).x;
> }
>
>
>
Previous Topic:Table: cannot select any cell that is not first column
Next Topic:Hand InputStream to SWT Browser?
Goto Forum:
  


Current Time: Fri Apr 26 01:22:34 GMT 2024

Powered by FUDForum. Page generated in 0.03330 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top