Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3fcaa1c65a4aaf91ff8ed9c43b54d8b9c3164319 (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
/*******************************************************************************
 * 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.define.dialogs;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.osee.framework.core.data.TestRunStorageKey;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.ui.plugin.util.AWorkbench;
import org.eclipse.osee.framework.ui.skynet.panels.BranchSelectSimpleComposite;
import org.eclipse.osee.framework.ui.swt.ImageManager;
import org.eclipse.osee.ote.ui.define.OteDefineImage;
import org.eclipse.osee.ote.ui.define.OteUiDefinePlugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

/**
 * @author Roberto E. Escobar
 */
public class BranchComboDialog extends TitleAreaDialog implements Listener {
   private static final Image TITLE_BAR_IMAGE = ImageManager.getImage(OteDefineImage.CHILD_BRANCH);
   private static final Image MESSAGE_IMAGE = ImageManager.getImage(OteDefineImage.COMMIT_WIZ);
   private static final String MESSAGE_TITLE = "Select a Working Branch";
   private static final String TITLE_BAR_TEXT = "Working Branch";
   private static final String MESSAGE = "Select a working branch";

   protected static final int COMBO_HISTORY_LENGTH = 5;

   private BranchSelectSimpleComposite branchSelectComposite;

   private BranchComboDialog(Shell parentShell) {
      super(parentShell);
   }

   @Override
   protected Control createDialogArea(Composite parent) {
      Composite content = (Composite) super.createDialogArea(parent);

      branchSelectComposite = BranchSelectSimpleComposite.createWorkingBranchSelectComposite(content, SWT.NONE);
      restoreWidgetValues();
      branchSelectComposite.addListener(this);
      setTitle(MESSAGE_TITLE);
      setTitleImage(MESSAGE_IMAGE);
      setMessage(MESSAGE);
      getShell().setText(TITLE_BAR_TEXT);
      getShell().setImage(TITLE_BAR_IMAGE);

      return branchSelectComposite;
   }

   @Override
   protected Control createButtonBar(Composite parent) {
      Control toReturn = super.createButtonBar(parent);
      checkState();
      return toReturn;
   }

   public Branch getSelection() {
      return branchSelectComposite.getSelectedBranch();
   }

   private boolean isValid() {
      return getSelection() != null;
   }

   private void checkState() {
      getButton(IDialogConstants.OK_ID).setEnabled(isValid());
      if (isValid() != true) {
         setErrorMessage("Branch cannot be empty.");
      } else {
         setErrorMessage(null);
      }
   }

   @Override
   protected void okPressed() {
      saveWidgetValues();
      super.okPressed();
   }

   @Override
   public void handleEvent(Event event) {
      checkState();
   }

   protected void restoreWidgetValues() {
      IDialogSettings settings = OteUiDefinePlugin.getInstance().getDialogSettings();
      if (settings != null) {
         String[] branchIds = settings.getArray(TestRunStorageKey.BRANCH_IDS);
         String lastSelected = settings.get(TestRunStorageKey.SELECTED_BRANCH_ID);
         branchSelectComposite.restoreWidgetValues(branchIds, lastSelected);
      }
   }

   protected void saveWidgetValues() {
      IDialogSettings settings = OteUiDefinePlugin.getInstance().getDialogSettings();
      if (settings != null) {
         // update source names history
         String[] branchIds = settings.getArray(TestRunStorageKey.BRANCH_IDS);
         if (branchIds == null) {
            branchIds = new String[0];
         }

         try {
            Branch branch = getSelection();
            if (branch != null && branch.hasParentBranch()) {
               String lastBranchSelected = Integer.toString(branch.getId());

               List<String> history = new ArrayList<String>(Arrays.asList(branchIds));
               history.remove(lastBranchSelected);
               history.add(0, lastBranchSelected);
               if (history.size() > COMBO_HISTORY_LENGTH) {
                  history.remove(COMBO_HISTORY_LENGTH);
               }
               branchIds = new String[history.size()];
               history.toArray(branchIds);

               settings.put(TestRunStorageKey.BRANCH_IDS, branchIds);
               settings.put(TestRunStorageKey.SELECTED_BRANCH_ID, lastBranchSelected);
               try {
                  settings.save(this.getClass().getName());
               } catch (IOException ex) {
                  OseeLog.log(OteUiDefinePlugin.class, Level.SEVERE, ex);
               }
            }
         } catch (OseeCoreException ex) {
            OseeLog.log(OteUiDefinePlugin.class, Level.SEVERE, ex);
         }
      }
   }

   public static Branch getBranchFromUser() throws OseeCoreException {
      Branch toReturn = null;
      BranchComboDialog branchSelection = new BranchComboDialog(AWorkbench.getActiveShell());
      int result = branchSelection.open();
      if (result == Window.OK) {
         toReturn = branchSelection.getSelection();
         if (toReturn != null && toReturn.hasParentBranch() == false) {
            toReturn = null;
         }
      }
      return toReturn;
   }
}

Back to the top