Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9e4cbcc7ae191eec62a8dc81b0b30002fabe39b4 (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
/*******************************************************************************
 * Copyright (c) 2010 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.message.watch.action;

import java.util.List;
import java.util.logging.Level;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.Window;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.osee.ote.message.Message;
import org.eclipse.osee.ote.message.elements.Element;
import org.eclipse.osee.ote.message.elements.EnumeratedElement;
import org.eclipse.osee.ote.message.tool.MessageMode;
import org.eclipse.osee.ote.ui.message.internal.Activator;
import org.eclipse.osee.ote.ui.message.tree.ElementNode;
import org.eclipse.osee.ote.ui.message.tree.WatchedMessageNode;
import org.eclipse.ui.dialogs.ListDialog;

/**
 * @author Ken J. Aguilar
 */
public class SetValueAction extends Action {
   private final WatchedMessageNode msgNode;
   private final ElementNode node;

   public SetValueAction(ElementNode node) {
      super("Set Value");
      this.node = node;
      msgNode = (WatchedMessageNode) node.getMessageNode();
      setEnabled(node.isEnabled() && msgNode.getSubscription().getMessageMode() == MessageMode.WRITER && msgNode.getSubscription().isActive());
   }

   @Override
   public void run() {
      Message<?, ?, ?> msg = msgNode.getSubscription().getMessage();
      List<Object> path = node.getElementPath().getElementPath();
      Element element = msg.getElement(path);
      if (element instanceof EnumeratedElement<?>) {
         final EnumeratedElement<?> enumElement = (EnumeratedElement<?>) element;
         try {
            final Enum<?>[] values = enumElement.getEnumValues();
            int width = 0;
            for (Enum<?> val : values) {
               width = val.toString().length() > width ? val.toString().length() : width;
            }

            final ListDialog dialog = new ListDialog(Displays.getActiveShell());
            dialog.setInput(values);
            dialog.setTitle("Set Value");
            dialog.setAddCancelButton(true);
            dialog.setWidthInChars(width + 5);
            dialog.setMessage(element.getFullName() + "\nSelect New Value");
            dialog.setContentProvider(new ITreeContentProvider() {

               @Override
               public Object[] getChildren(Object parentElement) {
                  return null;
               }

               @Override
               public Object getParent(Object element) {
                  return values;
               }

               @Override
               public boolean hasChildren(Object element) {
                  return false;
               }

               @Override
               public Object[] getElements(Object inputElement) {
                  return values;
               }

               @Override
               public void dispose() {
               }

               @Override
               public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
               }

            });

            dialog.setLabelProvider(new LabelProvider() {
               @Override
               public boolean isLabelProperty(Object element, String property) {
                  return false;
               }
            });

            int result = dialog.open();
            if (result == Window.OK) {
               final Object[] objs = dialog.getResult();
               if (objs.length == 1) {
                  final String value = ((Enum<?>) objs[0]).name();
                  msgNode.getSubscription().setElementValue(path, value);
               }
            }
         } catch (Throwable t) {
            final String logMsg =
               String.format("Exception while attempting to set element %s of message %s", element.getName(),
                  msg.getName());
            OseeLog.log(Activator.class, Level.SEVERE, logMsg, t);
         }

      } else {
         InputDialog dialog =
            new InputDialog(Displays.getActiveShell(), "Set " + element.getFullName(), "Enter set value", "0",
               new IInputValidator() {

                  @Override
                  public String isValid(String newText) {
                     // No Error accept all values;
                     return null;
                  }
               });

         //         final EntryDialog dialog =
         //               new EntryDialog(Displays.getActiveShell(), "Set " + element.getFullName(), null,
         //                     "Enter set value", MessageDialog.QUESTION, new String[] {"Ok", "Cancel"}, 0);
         if (dialog.open() == 0) {
            try {
               final String val = dialog.getValue();
               msgNode.getSubscription().setElementValue(path, val);
            } catch (Exception ex) {
               OseeLog.log(Activator.class, Level.SEVERE,
                  String.format("Unable to set the %s element for the message %s", element.getName(), msg.getName()),
                  ex);
               MessageDialog.openError(Displays.getActiveShell(), "Error", "Could not set value");
            }
         }
      }
   }
}

Back to the top