Skip to main content
summaryrefslogtreecommitdiffstats
blob: 96dcab31f9668e22514ba5623ccc99824b3b35d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * 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.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.ote.ui.host.cmd;

import java.util.Hashtable;
import java.util.logging.Level;
import net.jini.core.lookup.ServiceItem;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.osee.framework.jdk.core.type.InputManager;
import org.eclipse.osee.framework.jdk.core.type.TreeParent;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.ui.service.control.renderer.IServiceRenderer;
import org.eclipse.osee.framework.ui.swt.Widgets;
import org.eclipse.osee.ote.core.environment.interfaces.IHostTestEnvironment;
import org.eclipse.osee.ote.core.environment.interfaces.IRemoteCommandConsole;
import org.eclipse.osee.ote.core.environment.interfaces.ITestEnvironment;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

/**
 * Created on Oct 9, 2006
 */
public class TestServerConsoleServiceRenderer implements IServiceRenderer {
   private IHostTestEnvironment testService;
   private Group composite;
   private Hashtable<ITestEnvironment, IRemoteCommandConsole> consoles;
   private volatile IRemoteCommandConsole selectedConsole;
   private Text outputTxt;
   private Text inputTxt;
   private Button sendCmdBtn;
   private EnvironmentViewer envViewer;
   private InputManager<TreeParent> inputManager;

   public TestServerConsoleServiceRenderer() {
      this.consoles = new Hashtable<ITestEnvironment, IRemoteCommandConsole>(24);
      this.selectedConsole = null;
      this.inputManager = new InputManager<TreeParent>();
   }

   public void refresh() {
      Display.getCurrent().asyncExec(new Runnable() {

         public void run() {
            if (composite != null && !composite.isDisposed() && testService != null) {
               cleanupService();
               try {
                  ITestEnvironment[] envs = testService.getRemoteEnvironments();
                  consoles.clear();
                  if (envs.length > 0) {
                     for (ITestEnvironment env : envs) {
                        consoles.put(env, env.getCommandConsole());
                     }
                     selectedConsole = consoles.get(envs[0]);
                  } else {
                     selectedConsole = null;
                  }

                  envViewer.setInput(inputManager.getInputList());
                  TreeBuilder.buildTree(inputManager, testService, consoles);
                  envViewer.refresh();

               } catch (Throwable t) {
                  OseeLog.log(UiPlugin.class, Level.SEVERE, "exception getting hosts", t);
               }
            }
         }
      });
   }

   public void setService(ServiceItem serviceItem) {
      OseeLog.log(UiPlugin.class, Level.INFO, "setting test environment service");
      testService = (IHostTestEnvironment) serviceItem.service;
      if (outputTxt != null && !outputTxt.isDisposed()) {
         outputTxt.setText("");
      }
   }

   public void disconnect() {
      consoles.clear();
   }

   public void dispose() {
      cleanupService();
      Widgets.disposeWidgets(composite, outputTxt, inputTxt, sendCmdBtn);
   }

   public Control renderInComposite(Composite parent) {
      composite = new Group(parent, SWT.NONE);
      composite.setLayout(new GridLayout());
      composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
      composite.setText("Test Server Console");

      SashForm sashForm = new SashForm(composite, SWT.NONE);
      sashForm.setLayout(new GridLayout());
      sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
      sashForm.setOrientation(SWT.VERTICAL);
      sashForm.SASH_WIDTH = 3;

      envViewer = new EnvironmentViewer(sashForm, SWT.NONE);
      envViewer.getViewer().addDoubleClickListener(new IDoubleClickListener() {

         public void doubleClick(DoubleClickEvent event) {
            Object element = ((IStructuredSelection) event.getSelection()).getFirstElement();
            if (element != null && element instanceof ConsoleNode) {
               selectedConsole = ((ConsoleNode) element).getConsole();
               outputTxt.setText("");
            }
         }

      });
      createConsoleArea(sashForm);

      sashForm.setWeights(new int[] {3, 7});
      return composite;
   }

   private void createConsoleArea(Composite parent) {
      Group composite = new Group(parent, SWT.NONE);
      Widgets.setFormLayout(composite, 5, 5);
      composite.setText("Interact");

      outputTxt =
            Widgets.createTxt(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.READ_ONLY | SWT.BORDER, "");
      outputTxt.setTabs(4);
      outputTxt.setBackground(PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_BLACK));
      outputTxt.setForeground(PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_GREEN));
      outputTxt.setFont(new Font(Display.getDefault(), "Courier", 10, SWT.NORMAL));
      Widgets.attachToParent(outputTxt, SWT.TOP, 0, 5);
      Widgets.attachToParent(outputTxt, SWT.BOTTOM, 75, 0);
      Widgets.attachToParent(outputTxt, SWT.LEFT, 0, 5);
      Widgets.attachToParent(outputTxt, SWT.RIGHT, 100, -5);

      inputTxt = Widgets.createTxt(composite, SWT.SINGLE | SWT.BORDER, "");
      inputTxt.addTraverseListener(new TraverseListener() {

         public void keyTraversed(TraverseEvent event) {
            if (event.detail == SWT.TRAVERSE_RETURN) {
               sendCmd(inputTxt.getText());
            }
         }

      });
      Widgets.attachToControl(inputTxt, outputTxt, SWT.LEFT, SWT.LEFT, 0);
      sendCmdBtn = Widgets.createBtn(composite, SWT.PUSH, "Execute");
      sendCmdBtn.addSelectionListener(new SelectionAdapter() {

         @Override
         public void widgetSelected(SelectionEvent arg0) {
            sendCmd(inputTxt.getText());
         }

      });
      Widgets.attachToControl(sendCmdBtn, outputTxt, SWT.TOP, SWT.BOTTOM, 5);
      Widgets.attachToControl(sendCmdBtn, outputTxt, SWT.RIGHT, SWT.RIGHT, 0);
      Widgets.attachToControl(inputTxt, sendCmdBtn, SWT.RIGHT, SWT.LEFT, -5);
      Widgets.attachToControl(inputTxt, sendCmdBtn, SWT.TOP, SWT.CENTER, 0);
   }

   private void sendCmd(String cmd) {
      try {
         if (selectedConsole != null) {
            outputTxt.append(selectedConsole.doCommand(cmd));
            inputTxt.setText("");
         } else {
            outputTxt.append("No Consoles Available\n");
         }
      } catch (Throwable t) {
         OseeLog.log(UiPlugin.class, Level.SEVERE, "Exception trying to execute test service console command " + cmd, t);
         outputTxt.append("Exception ocurred when executing command\n");

      }
   }

   private void cleanupService() {
      consoles.clear();
      inputManager.removeAll();
   }
}

Back to the top