Java/SWT JFace Eclipse/Status Bar — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:55, 1 июня 2010
SWT Status Bar Demo
/*
SWT/JFace in Action
GUI Design with Eclipse 3.0
Matthew Scarpino, Stephen Holder, Stanford Ng, and Laurent Mihalkovic
ISBN: 1932394273
Publisher: Manning
*/
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
public class Ch4_Contributions extends ApplicationWindow {
StatusLineManager slm = new StatusLineManager();
Ch4_StatusAction status_action = new Ch4_StatusAction(slm);
ActionContributionItem aci = new ActionContributionItem(status_action);
public Ch4_Contributions() {
super(null);
addStatusLine();
addMenuBar();
addToolBar(SWT.FLAT | SWT.WRAP);
}
protected Control createContents(Composite parent) {
getShell().setText("Action/Contribution Example");
parent.setSize(290, 150);
aci.fill(parent);
return parent;
}
public static void main(String[] args) {
Ch4_Contributions swin = new Ch4_Contributions();
swin.setBlockOnOpen(true);
swin.open();
Display.getCurrent().dispose();
}
protected MenuManager createMenuManager() {
MenuManager main_menu = new MenuManager(null);
MenuManager action_menu = new MenuManager("Menu");
main_menu.add(action_menu);
action_menu.add(status_action);
return main_menu;
}
protected ToolBarManager createToolBarManager(int style) {
ToolBarManager tool_bar_manager = new ToolBarManager(style);
tool_bar_manager.add(status_action);
return tool_bar_manager;
}
protected StatusLineManager createStatusLineManager() {
return slm;
}
}
class Ch4_StatusAction extends Action {
StatusLineManager statman;
short triggercount = 0;
public Ch4_StatusAction(StatusLineManager sm) {
super("&Trigger@Ctrl+T", AS_PUSH_BUTTON);
statman = sm;
setToolTipText("Trigger the Action");
setImageDescriptor(ImageDescriptor.createFromFile(this.getClass(),
"eclipse.gif"));
}
public void run() {
triggercount++;
statman.setMessage("The status action has fired. Count: "
+ triggercount);
}
}
Update a status line (Statusbar) when the pointer enters a ToolItem
/*
* ToolBar example snippet: update a status line when the pointer enters a ToolItem
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class Snippet153 {
static String statusText = "";
public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(10, 10, 200, 200);
final ToolBar bar = new ToolBar(shell, SWT.BORDER);
bar.setBounds(10, 10, 150, 50);
final Label statusLine = new Label(shell, SWT.BORDER);
statusLine.setBounds(10, 90, 150, 30);
new ToolItem(bar, SWT.NONE).setText("item 1");
new ToolItem(bar, SWT.NONE).setText("item 2");
new ToolItem(bar, SWT.NONE).setText("item 3");
bar.addMouseMoveListener(new MouseMoveListener() {
public void mouseMove(MouseEvent e) {
ToolItem item = bar.getItem(new Point(e.x, e.y));
String name = "";
if (item != null) {
name = item.getText();
}
if (!statusText.equals(name)) {
statusLine.setText(name);
statusText = name;
}
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Update a status line when an item is armed
/*
* Control example snippet: update a status line when an item is armed
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
public class Snippet152 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
FormLayout layout = new FormLayout();
shell.setLayout(layout);
final Label label = new Label(shell, SWT.BORDER);
Listener armListener = new Listener() {
public void handleEvent(Event event) {
MenuItem item = (MenuItem) event.widget;
label.setText(item.getText());
label.update();
}
};
Listener showListener = new Listener() {
public void handleEvent(Event event) {
Menu menu = (Menu) event.widget;
MenuItem item = menu.getParentItem();
if (item != null) {
label.setText(item.getText());
label.update();
}
}
};
Listener hideListener = new Listener() {
public void handleEvent(Event event) {
label.setText("");
label.update();
}
};
FormData labelData = new FormData();
labelData.left = new FormAttachment(0);
labelData.right = new FormAttachment(100);
labelData.bottom = new FormAttachment(100);
label.setLayoutData(labelData);
Menu menuBar = new Menu(shell, SWT.BAR);
shell.setMenuBar(menuBar);
MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE);
fileItem.setText("File");
fileItem.addListener(SWT.Arm, armListener);
MenuItem editItem = new MenuItem(menuBar, SWT.CASCADE);
editItem.setText("Edit");
editItem.addListener(SWT.Arm, armListener);
Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
fileMenu.addListener(SWT.Hide, hideListener);
fileMenu.addListener(SWT.Show, showListener);
fileItem.setMenu(fileMenu);
String[] fileStrings = { "New", "Close", "Exit" };
for (int i = 0; i < fileStrings.length; i++) {
MenuItem item = new MenuItem(fileMenu, SWT.PUSH);
item.setText(fileStrings[i]);
item.addListener(SWT.Arm, armListener);
}
Menu editMenu = new Menu(shell, SWT.DROP_DOWN);
editMenu.addListener(SWT.Hide, hideListener);
editMenu.addListener(SWT.Show, showListener);
String[] editStrings = { "Cut", "Copy", "Paste" };
editItem.setMenu(editMenu);
for (int i = 0; i < editStrings.length; i++) {
MenuItem item = new MenuItem(editMenu, SWT.PUSH);
item.setText(editStrings[i]);
item.addListener(SWT.Arm, armListener);
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}