Skip to main content
summaryrefslogtreecommitdiffstats
blob: bac444df98d8b9e786407f6d548f767058086264 (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
/*******************************************************************************
 * 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.framework.ui.skynet.widgets;

import org.eclipse.osee.framework.jdk.core.util.AHTML;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.ui.swt.ALayout;
import org.eclipse.osee.framework.ui.swt.Widgets;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Hyperlink;

/**
 * @author Donald G. Dunne
 */
public abstract class XHyperlinkLabelValueSelection extends GenericXWidget {

   Hyperlink valueHyperlinkLabel;
   protected Label valueLabel;

   public XHyperlinkLabelValueSelection(String label) {
      super(label);
   }

   /**
    * Override this method to provide changing value
    */
   public abstract String getCurrentValue();

   @Override
   protected void createControls(Composite parent, int horizontalSpan) {

      Composite comp = new Composite(parent, SWT.NONE);
      comp.setLayout(ALayout.getZeroMarginLayout(5, false));
      if (isFillHorizontally()) {
         comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
      } else {
         comp.setLayoutData(new GridData());
      }

      // Create Text Widgets
      if (isDisplayLabel() && !getLabel().equals("")) {
         labelWidget = new Label(comp, SWT.NONE);
         labelWidget.setText(getLabel() + ":");
         if (getToolTip() != null) {
            labelWidget.setToolTipText(getToolTip());
         }
      }

      GridData gd = new GridData();
      if (isFillHorizontally()) {
         gd.grabExcessHorizontalSpace = true;
      }

      if (isEditable()) {
         if (toolkit == null) {
            valueHyperlinkLabel = new Hyperlink(comp, SWT.NONE);
         } else {
            valueHyperlinkLabel = toolkit.createHyperlink(comp, " <edit>", SWT.NONE);
         }
         valueHyperlinkLabel.setToolTipText(Strings.isValid(getToolTip()) ? getToolTip() : "Select to Modify");
         valueHyperlinkLabel.setLayoutData(gd);
         valueHyperlinkLabel.addListener(SWT.MouseUp, new Listener() {
            @Override
            public void handleEvent(Event event) {
               if (handleSelection()) {
                  refresh();
                  notifyXModifiedListeners();
               }
            }
         });
      } else {
         if (toolkit == null) {
            valueLabel = new Label(comp, SWT.NONE);
         } else {
            valueLabel = toolkit.createLabel(comp, " Not Set", SWT.NONE);
         }
         valueLabel.setToolTipText(getToolTip());
         valueLabel.setLayoutData(gd);
      }

      refresh();
   }

   @Override
   public void refresh() {
      if (getControl() == null || getControl().isDisposed()) {
         return;
      }
      if (Widgets.isAccessible(valueHyperlinkLabel)) {
         if (getCurrentValue().equals(valueHyperlinkLabel.getText())) {
            return;
         }
         valueHyperlinkLabel.setText(getCurrentValue());
         valueHyperlinkLabel.update();
         valueHyperlinkLabel.getParent().update();
      } else if (Widgets.isAccessible(valueLabel)) {
         if (getCurrentValue().equals(valueLabel.getText())) {
            return;
         }
         valueLabel.setText(getCurrentValue());
         valueLabel.update();
         valueLabel.getParent().update();

      }
      validate();
   }

   public boolean handleSelection() {
      return false;
   }

   @Override
   public Control getControl() {
      if (Widgets.isAccessible(valueHyperlinkLabel)) {
         return valueHyperlinkLabel;
      }
      if (Widgets.isAccessible(valueLabel)) {
         return valueLabel;
      }
      return null;
   }

   @Override
   public void adaptControls(FormToolkit toolkit) {
      super.adaptControls(toolkit);
      if (Widgets.isAccessible(valueHyperlinkLabel)) {
         toolkit.adapt(valueHyperlinkLabel, true, true);
         valueHyperlinkLabel.update();
      }
      if (Widgets.isAccessible(valueLabel)) {
         toolkit.adapt(valueLabel, true, true);
         valueLabel.update();
      }
   }

   @Override
   public String toHTML(String labelFont) {
      return AHTML.getLabelValueStr(AHTML.LABEL_FONT, getLabel(), getCurrentValue());
   }
}

Back to the top