Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0b9f8b664e87495eff1b557c9ada7d88a7a3cb8e (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
/*******************************************************************************
 * 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.test.manager.panels;

import java.io.File;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.ui.swt.ImageManager;
import org.eclipse.osee.ote.ui.test.manager.OteTestManagerImage;
import org.eclipse.osee.ote.ui.test.manager.util.Dialogs;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
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.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

/**
 * @author Roberto E. Escobar
 */
public class FileOrDirectorySelectionPanel extends Composite {

   private static final Image FILE_SELECT_IMAGE = ImageManager.getImage(OteTestManagerImage.FILE);
   private static final String DIRECTORY_ERROR_MESSAGE = "Directory should be blank or set an accessible directory.";
   private static final String FILE_ERROR_MESSAGE = "Unable to access file.";
   private static final String DEFAULT_FILE = "/dev/null";

   private StyledText textField;
   private Label labelField;
   private final String labelText;
   private final String toolTipText;
   private final boolean isDirectory;

   public FileOrDirectorySelectionPanel(Composite parent, int style, String labelText, String toolTipText, boolean isDirectory) {
      super(parent, style);
      this.labelText = labelText;
      this.toolTipText = toolTipText;
      this.isDirectory = isDirectory;
      GridLayout gl = new GridLayout(3, false);
      gl.marginHeight = 0;
      gl.marginWidth = 0;
      this.setLayout(gl);
      this.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
      createControl(this);
   }

   private void createControl(Composite parent) {
      labelField = new Label(parent, SWT.NONE);
      labelField.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, false, false));
      labelField.setText(labelText);
      labelField.setToolTipText(toolTipText);

      textField = new StyledText(parent, SWT.BORDER);
      textField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

      if (isDirectory != true) {
         textField.setText(DEFAULT_FILE);
      }
      Button fileDialog = new Button(parent, SWT.NONE);
      fileDialog.setLayoutData(new GridData(SWT.FILL, SWT.END, false, false));
      fileDialog.setImage(FILE_SELECT_IMAGE);
      fileDialog.addSelectionListener(new SelectionAdapter() {
         @Override
         public void widgetSelected(SelectionEvent e) {
            createDialog();
         }
      });

   }

   private void createDialog() {
      Shell shell = PlatformUI.getWorkbench().getDisplay().getActiveShell();
      String selection = "";
      if (isDirectory == true) {
         DirectoryDialog directoryDialog = new DirectoryDialog(shell, SWT.OPEN);

         String defaultDir = textField.getText();
         File dir = new File(defaultDir);
         if (dir.isFile() || dir.isDirectory()) {
            directoryDialog.setFilterPath(defaultDir);
         } else {
            directoryDialog.setFilterPath("Y:\\");
         }
         selection = directoryDialog.open();
      } else {
         FileDialog fileDialog = new FileDialog(shell, SWT.OPEN);
         selection = fileDialog.open();
      }
      setSelected(selection);
   }

   private void verifySelection() {
      String text = getSelected();
      if (isValid() != true) {
         if (isDirectory == true) {
            text = "";
         } else {
            text = DEFAULT_FILE;
         }
         Dialogs.popupError("Error", getErrorMessage());
      }
      if (textField.getText().equals(text) != true) {
         textField.setText(text);
      }
   }

   private boolean isValidFile(String text) {
      File file = new File(text);
      return file != null && file.exists() != false && file.canWrite() != false;
   }

   public boolean isValid() {
      boolean toReturn = false;
      String text = getSelected();
      if (isDirectory == true) {
         toReturn = Strings.isValid(text) == true ? isValidFile(text) : true;
      } else {
         //         if (Strings.isValid(text) == true) {
         //            if (text.equals(DEFAULT_FILE) == true) {
         toReturn = true;
         //            } else {
         //               toReturn = isValidFile(text);
         //            }
         //         }
      }
      return toReturn;
   }

   public String getErrorMessage() {
      return isDirectory == true ? DIRECTORY_ERROR_MESSAGE : FILE_ERROR_MESSAGE;
   }

   public String getSelected() {
      return textField != null && textField.isDisposed() != true ? textField.getText() : "";
   }

   public void setSelected(String value) {
      if (textField != null && textField.isDisposed() != true) {
         if (Strings.isValid(value)) {
            textField.setText(value);
         }
         verifySelection();
      }
   }
}

Back to the top