📄 ngdropdown.java
字号:
/*******************************************************************************
* Copyright (c) 2004 Stefan Zeiger and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.novocode.com/legal/epl-v10.html
*
* Contributors:
* Stefan Zeiger (szeiger@novocode.com) - initial API and implementation
*******************************************************************************/
package com.novocode.naf.gui;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import com.novocode.naf.app.*;
import com.novocode.naf.data.DataDecoder;
import com.novocode.naf.data.ModelBinding;
import com.novocode.naf.data.XMLProperty;
import com.novocode.naf.gui.event.ActionEvent;
import com.novocode.naf.gui.event.DefaultActionBroadcaster;
import com.novocode.naf.gui.event.IActionListener;
import com.novocode.naf.gui.event.ChangeEvent;
import com.novocode.naf.gui.event.IChangeListener;
import com.novocode.naf.model.DefaultItemListModel;
import com.novocode.naf.model.IItemListModel;
import com.novocode.naf.resource.NGComponent;
/**
* A drop-down menu button.
*
* @author Stefan Zeiger (szeiger@novocode.com)
* @since Dec 4, 2003
*/
public final class NGDropDown extends NGWidget implements IToolItemWidget
{
private String text, imageResource;
@XMLProperty public void setText(String s) { this.text = s;}
public String getText() { return text; }
@XMLProperty("image") public void setImageResource(String s) { this.imageResource = s; }
public String getImageResource() { return imageResource; }
public ToolItem createToolItem(NGToolBar parentComp, final ToolBar bar, final ShellWindowInstance wi) throws NAFException
{
final ToolItem item = new ToolItem(bar, SWT.DROP_DOWN);
parentComp.addRegisteredImage(imageResource, item, wi.imageManager);
if(text != null) item.setText(DataDecoder.decodeAccessKey(text));
else if((bar.getStyle() & SWT.RIGHT) != 0) item.setText("");
final IActionListener actionModel = getModel("action", wi.models);
final ActionEvent ae = (actionModel == null) ? null : new ActionEvent(this, getModelBinding("action").getAttribute("command"), wi);
IItemListModel itemsModel = getModel("items", wi.models);
final Menu[] menu = new Menu[1];
if(itemsModel != null)
{
final boolean[] dirty = new boolean[1];
final IChangeListener cl = new IChangeListener()
{
public void stateChanged(ChangeEvent e)
{
dirty[0] = true;
}
};
SWTUtil.registerModel(item, itemsModel, cl);
item.addListener(SWT.Selection, new Listener ()
{
public void handleEvent (Event event)
{
if(event.detail == SWT.ARROW)
{
if(menu[0] == null || dirty[0])
{
dirty[0] = false;
if(menu[0] != null)
{
//System.out.println("Disposing of dynamic menu");
menu[0].dispose();
}
//System.out.println("Creating dynamic menu");
menu[0] = new Menu (wi.getShell(true), SWT.POP_UP);
addMenuChildren(menu[0], wi);
}
Rectangle rect = item.getBounds();
Point pt = bar.toDisplay(new Point(rect.x, rect.y + rect.height));
menu[0].setLocation(pt.x, pt.y);
menu[0].setVisible(true);
}
else if(actionModel != null)
{
actionModel.performAction(ae);
}
}
});
}
else // no dynamic items
{
menu[0] = new Menu (wi.getShell(true), SWT.POP_UP);
item.addListener(SWT.Selection, new Listener ()
{
public void handleEvent (Event event)
{
if(event.detail == SWT.ARROW)
{
Rectangle rect = item.getBounds();
Point pt = bar.toDisplay(new Point(rect.x, rect.y + rect.height));
menu[0].setLocation(pt.x, pt.y);
menu[0].setVisible(true);
}
else if(actionModel != null)
{
actionModel.performAction(ae);
}
}
});
addMenuChildren(menu[0], wi);
}
item.addListener(SWT.Dispose, new Listener()
{
public void handleEvent(Event event)
{
if(menu[0] != null)
{
menu[0].dispose();
menu[0] = null;
}
}
});
//addModels(item, wi);
return item;
}
private void addMenuChildren(Menu parent, ShellWindowInstance wi)
{
ModelBinding itemsModelBinding = getModelBinding("items");
IItemListModel itemsModel = getModel("items", wi.models);
//System.out.println("itemsModel = "+itemsModel);
//System.out.println("itemsModelBinding.widgetListIdx = "+itemsModelBinding.widgetListIdx);
List<NGComponent> list = getUnexpandedChildren(null);
List<NGComponent> childList = new ArrayList<NGComponent>();
for(int i=0; i<=list.size(); i++)
{
if(itemsModel != null && itemsModelBinding.widgetListIdx == i)
{
NGComponent[] dynamicWidgets = itemsModel.getItems();
childList.clear();
//System.out.println("dynamicWidgets.length = "+dynamicWidgets.length);
for(int j=0; j<dynamicWidgets.length; j++) dynamicWidgets[j].expandSelf(childList);
//System.out.println("childList.size() = "+childList.size());
for(NGComponent ch : childList)
{
if(!(ch instanceof IMenuItemWidget))
throw new NAFException("Cannot create a menu item from a "+ch.getClass()+" object");
MenuItem item = ((IMenuItemWidget)ch).createMenuItem(parent, wi);
((NGWidget)ch).addModels(item, wi, this);
}
}
if(i != list.size())
{
childList.clear();
list.get(i).expandSelf(childList);
for(NGComponent ch : childList)
{
if(!(ch instanceof IMenuItemWidget))
throw new NAFException("Cannot create a menu item from a "+ch.getClass()+" object");
MenuItem item = ((IMenuItemWidget)ch).createMenuItem(parent, wi);
((NGWidget)ch).addModels(item, wi, this);
}
}
}
}
protected Object createDefaultModel(ModelBinding mb)
{
if("items".equals(mb.type)) return new DefaultItemListModel();
else if("action".equals(mb.type)) return new DefaultActionBroadcaster(mb.id);
else return super.createDefaultModel(mb);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -