Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: irate/swt/plugin/lircremote/LircRemoteControlConfigurator.java


1   // Copyright 2003 Stephen Blackheath
2   
3   package irate.swt.plugin.lircremote;
4   
5   import org.eclipse.swt.widgets.*;
6   import org.eclipse.swt.layout.*;
7   import org.eclipse.swt.events.*;
8   import org.eclipse.swt.SWT;
9   import irate.plugin.*;
10  import irate.plugin.lircremote.LircRemoteControlPlugin;
11  import irate.plugin.lircremote.LircRemoteControlListener;
12  import irate.plugin.lircremote.Function;
13  
14  /**
15   * SWT version of the configurator for the Lirc remote control.
16   *
17   * @author Stephen Blackheath
18   */
19  public class LircRemoteControlConfigurator
20    implements LircRemoteControlListener
21  {
22    private Display display;
23    private PluginApplication app;
24    private PluginManager pluginManager;
25    private LircRemoteControlPlugin plugin;
26    private boolean done = false;
27    private Text host;
28    private Text port;
29    private Label status;
30  
31    public LircRemoteControlConfigurator(Display display_, PluginApplication app_, Plugin plugin_)
32    {
33      this.display = display_;
34      this.app = app_;
35      this.plugin = (LircRemoteControlPlugin) plugin_;
36  
37      plugin.addLircRemoteControlListener(this);
38      boolean wasAttached = plugin.isAttached();
39        // Detach from the application while configuring so the remote control
40        // buttons don't affect the application.
41      if (wasAttached)
42        plugin.detach();
43  
44      final Shell shell = new Shell(display);
45      shell.setText("Lirc remote control configuration");
46      shell.addShellListener(new ShellAdapter()
47      {
48        public void shellClosed(ShellEvent e){
49          done=true;
50        }
51      });
52      GridLayout layout = new GridLayout(2, false);
53      shell.setLayout(layout);
54  
55      new Label(shell, SWT.NONE).setText("Host");
56      host = new Text(shell, SWT.SINGLE | SWT.BORDER);
57      host.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));;
58      host.setText(plugin.getHost());
59      ModifyListener callSetup = new ModifyListener() {
60        public void modifyText(ModifyEvent e) {
61    setup();
62        }
63      };
64      host.addModifyListener(callSetup);
65  
66      new Label(shell, SWT.NONE).setText("Port");
67      port = new Text(shell, SWT.SINGLE | SWT.BORDER);
68      port.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));;
69      port.setText(Integer.toString(plugin.getPort()));
70      port.addModifyListener(callSetup);
71  
72      new Label(shell, SWT.NONE).setText("Status");
73      status = new Label(shell, SWT.SINGLE | SWT.BORDER);
74      setStatus();
75  
76      for (int i = 0; i < plugin.getFunctions().size(); i++) {
77        final Function func = (Function) plugin.getFunctions().get(i);
78        new Label(shell, SWT.NONE).setText(func.getName());
79        org.eclipse.swt.widgets.Button setUp = new org.eclipse.swt.widgets.Button(shell, SWT.NONE);
80        setUp.setText("Set up");
81        setUp.addSelectionListener(new SelectionAdapter(){
82    public void widgetSelected(SelectionEvent e){
83            new FunctionSetup(display, app, plugin, func);
84          }
85        });
86      }
87  
88      org.eclipse.swt.widgets.Button ok = new org.eclipse.swt.widgets.Button(shell, SWT.NONE);
89      ok.setText("OK");
90      GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
91      gd.horizontalSpan = 2;
92      ok.setLayoutData(gd);
93      ok.addSelectionListener(new SelectionAdapter(){
94        public void widgetSelected(SelectionEvent e){
95          setup();
96          shell.close();
97        }
98      });
99      shell.pack();    
100     shell.open();
101     while (!done) {
102       if (!display.readAndDispatch()) display.sleep();
103     } 
104 
105     if (wasAttached)
106       plugin.attach(app);
107     plugin.removeLircRemoteControlListener(this);
108 
109     shell.dispose();
110   }
111 
112 
113   private void setup()
114   {
115     plugin.setHost(host.getText());
116     try {
117       if (!done)
118   plugin.setPort(Integer.parseInt(port.getText()));
119     }
120     catch (NumberFormatException e) {
121     }
122   }
123 
124   private void setStatus()
125   {
126     if (!done)
127       status.setText(plugin.getConnectStatus() ? "connected" : "disconnected");
128   }
129 
130   public void connectStatusChanged(LircRemoteControlPlugin plugin, boolean connected)
131   {
132     if (!done)
133       display.asyncExec(new Runnable() {
134   public void run() {
135     setStatus();
136   }
137       });
138   }
139 
140   public void buttonPressed(LircRemoteControlPlugin plugin, irate.plugin.lircremote.Button button)
141   {
142   }
143 }