root/jface/ActionAndStatusbar.d

Revision 171:0a21c7cabc16, 4.8 kB (checked in by Frank Benoit <benoit@tionex.de>, 8 months ago)

presentation windows

Line 
1 /*
2 DWT/JFace in Action
3 GUI Design with Eclipse 3.0
4 Matthew Scarpino, Stephen Holder, Stanford Ng, and Laurent Mihalkovic
5
6 ISBN: 1932394273
7
8 Publisher: Manning
9
10 Port to the D programming language:
11     Frank Benoit <benoit@tionex.de>
12 Added some stuff to play with the statusbar and its progressmonitor
13
14 */
15 module jface.ActionAndStatusbar;
16
17 import dwtx.jface.action.Action;
18 import dwtx.jface.action.ActionContributionItem;
19 import dwtx.jface.action.MenuManager;
20 import dwtx.jface.action.StatusLineManager;
21 import dwtx.jface.action.ToolBarManager;
22 import dwtx.jface.action.Separator;
23 import dwtx.jface.resource.ImageDescriptor;
24 import dwtx.jface.window.ApplicationWindow;
25 import dwtx.core.runtime.IProgressMonitor;
26 import dwt.DWT;
27 import dwt.widgets.Composite;
28 import dwt.widgets.Control;
29 import dwt.widgets.Display;
30 import dwt.widgets.Group;
31 import dwt.widgets.Button;
32 import dwt.widgets.Event;
33 import dwt.widgets.Listener;
34 import dwt.widgets.Text;
35 import dwt.layout.GridData;
36 import dwt.layout.GridLayout;
37
38 import tango.text.convert.Format;
39 version(JIVE) import jive.stacktrace;
40 import dwt.dwthelper.utils;
41 import dwt.dwthelper.Runnable;
42
43 void main() {
44     auto swin = new ActionAndStatusbar();
45     swin.setBlockOnOpen(true);
46     swin.open();
47     Display.getCurrent().dispose();
48 }
49
50 public class ActionAndStatusbar : ApplicationWindow {
51     StatusLineManager slm;
52
53     StatusAction status_action;
54
55     ActionContributionItem aci;
56
57     public this() {
58         super(null);
59         slm = new StatusLineManager();
60         status_action = new StatusAction(slm);
61         aci = new ActionContributionItem(status_action);
62         addStatusLine();
63         addMenuBar();
64         addToolBar(DWT.FLAT | DWT.WRAP);
65     }
66
67     protected Control createContents(Composite parent) {
68         getShell().setText("Action/Contribution Example");
69         //parent.setSize(290, 150);
70         //aci.fill(parent);
71         Composite comp = cast(Composite) super.createContents(parent);
72         comp.setLayout( new GridLayout());
73         new Text( comp, DWT.BORDER );
74         auto grp = new Group(comp, DWT.None);
75         grp.setLayoutData( new GridData( GridData.FILL_BOTH ));
76         //grp.setSize( 200, 200 );
77         grp.setText("Action enable" );
78         grp.setLayout(new GridLayout( 1, true ));
79         with( new Button( grp, DWT.RADIO )){
80             setText( "On" );
81             addListener( DWT.Selection, dgListener( &doEnable, true ));
82         }
83         with( new Button( grp, DWT.RADIO )){
84             setText( "Off" );
85             addListener( DWT.Selection, dgListener( &doEnable, false ));
86         }
87         return comp;
88     }
89     void doEnable( Event e, bool state ){
90         status_action.setEnabled( state );
91     }
92
93     protected MenuManager createMenuManager() {
94         MenuManager main_menu = new MenuManager(null);
95         MenuManager file_menu = new MenuManager("File");
96         MenuManager action_menu = new MenuManager("Menu");
97         main_menu.add(file_menu);
98         main_menu.add(action_menu);
99         action_menu.add(status_action);
100         file_menu.add(status_action);
101         return main_menu;
102     }
103
104     protected ToolBarManager createToolBarManager(int style) {
105         ToolBarManager tool_bar_manager = new ToolBarManager(style);
106         tool_bar_manager.add(status_action);
107         tool_bar_manager.add(new Separator());
108         tool_bar_manager.add(status_action);
109         return tool_bar_manager;
110     }
111
112     protected StatusLineManager createStatusLineManager() {
113         return slm;
114     }
115 }
116
117 class StatusAction : Action {
118     StatusLineManager statman;
119
120     short triggercount = 0;
121
122     public this(StatusLineManager sm) {
123         super("&Trigger@Ctrl+T", AS_PUSH_BUTTON);
124         statman = sm;
125         setToolTipText("Trigger the Action");
126         setImageDescriptor(ImageDescriptor.createFromFile(
127             getImportData!("eclipse-icon-red-16.png")));
128     }
129
130     public void run() {
131         triggercount++;
132         statman.setMessage( Format("The status action has fired. Count: {}", triggercount));
133
134         if( triggercount % 5 == 0 ){
135             statman.setCancelEnabled(true);
136             auto pm = statman.getProgressMonitor();
137             pm.setCanceled(false);
138             pm.beginTask( "Task", 100 );
139             auto runner = new class(pm) Runnable {
140                 IProgressMonitor pm;
141                 int w;
142                 this(IProgressMonitor a){
143                     this.pm = a;
144                 }
145                 void run(){
146                     const incr = 2;
147                     this.pm.worked( incr );
148                     w += incr;
149                     if( w < 100 && !this.pm.isCanceled() ){
150                         Display.getCurrent().timerExec( 100, this);
151                     }
152                     else{
153                         this.pm.done();
154                     }
155                 }
156             };
157             Display.getCurrent().syncExec(runner);
158         }
159     }
160 }
Note: See TracBrowser for help on using the browser.