Skip to main content
summaryrefslogtreecommitdiffstats
blob: a33fb96958ba4cfd90462e9715dd00ce9835e85e (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*******************************************************************************
 * 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.search;

import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.plugin.core.util.Jobs;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.Attribute;
import org.eclipse.osee.framework.skynet.core.attribute.AttributeTypeManager;
import org.eclipse.osee.framework.skynet.core.utility.Artifacts;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Dialog for performing find/replace of attribute values on a set of <code>Artifact</code>'s.
 * 
 * @see org.eclipse.osee.framework.skynet.core.artifact.Artifact
 * @see org.eclipse.osee.framework.skynet.core.artifact.Attribute
 * @author Robert A. Fisher
 */
public class AttributeFindReplaceDialog extends Dialog {
   private ComboViewer cmbAttributeDescriptors;
   private Text txtFindRegEx;
   private Text txtReplaceStr;
   private BranchId branch;

   private final List<Artifact> artifacts;

   public AttributeFindReplaceDialog(Shell parentShell, List<Artifact> artifacts) {
      super(parentShell);

      this.artifacts = artifacts;
      if (artifacts != null && !artifacts.isEmpty()) {
         this.branch = artifacts.get(0).getBranch();
      }
      setShellStyle(SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | getDefaultOrientation() | SWT.RESIZE);
   }

   @Override
   protected Control createDialogArea(Composite parent) {
      getShell().setText("Find/Replace Attribute Value");

      Composite mainComposite = new Composite(parent, SWT.NONE);
      mainComposite.setFont(parent.getFont());
      mainComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
      mainComposite.setLayout(new GridLayout(1, false));

      addDialogControls(mainComposite);
      addListeners();
      setInputs();

      return mainComposite;
   }

   @Override
   protected void createButtonsForButtonBar(Composite parent) {
      super.createButtonsForButtonBar(parent);
      checkEnabled();
   }

   private void setInputs() {
      try {
         Collection<IAttributeType> attributeTypes = AttributeTypeManager.getValidAttributeTypes(branch);
         cmbAttributeDescriptors.setInput(attributeTypes.toArray(new IAttributeType[attributeTypes.size()]));
         cmbAttributeDescriptors.getCombo().select(0);
      } catch (OseeCoreException ex) {
         cmbAttributeDescriptors.setInput(new Object[] {ex});
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      }
   }

   private void addListeners() {
      txtFindRegEx.addModifyListener(new ModifyListener() {
         @Override
         public void modifyText(ModifyEvent e) {
            checkEnabled();
         }
      });
   }

   private void addDialogControls(Composite mainComposite) {
      Label label;
      label = new Label(mainComposite, SWT.LEFT);
      label.setText("Attribute Type");
      label.setToolTipText("The attribute to perform the find/replace logic against");

      cmbAttributeDescriptors = new ComboViewer(mainComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
      cmbAttributeDescriptors.setContentProvider(new ArrayContentProvider());
      cmbAttributeDescriptors.setLabelProvider(new ArtifactTypeLabelProvider());
      cmbAttributeDescriptors.setSorter(new ViewerSorter() {
         @SuppressWarnings("unchecked")
         @Override
         public int compare(Viewer viewer, Object e1, Object e2) {
            return getComparator().compare(((AttributeType) e1).getName(), ((AttributeType) e2).getName());
         }
      });

      label = new Label(mainComposite, SWT.LEFT);
      label.setText("Find (regex):");
      label.setToolTipText("The regular expression to perform matching with against the attribute value");
      txtFindRegEx = new Text(mainComposite, SWT.BORDER);

      label = new Label(mainComposite, SWT.LEFT);
      label.setText("Replace With:");
      label.setToolTipText("The value to put in place of the value matched by the Find regular expression");
      txtReplaceStr = new Text(mainComposite, SWT.BORDER);

   }

   private void checkEnabled() {
      boolean enable = txtFindRegEx.getText().length() > 0 && !artifacts.isEmpty();
      getButton(IDialogConstants.OK_ID).setEnabled(enable);
   }

   @Override
   protected void okPressed() {
      final Pattern pattern = Pattern.compile(txtFindRegEx.getText());
      final String replaceText = txtReplaceStr.getText();
      final IAttributeType attributeType =
         (IAttributeType) ((IStructuredSelection) cmbAttributeDescriptors.getSelection()).getFirstElement();

      Job job = new Job("Find/Replace") {

         @Override
         protected IStatus run(final IProgressMonitor monitor) {
            IStatus toReturn = Status.CANCEL_STATUS;
            try {
               monitor.beginTask("Find/Replace " + attributeType + " Attribute Value", artifacts.size());

               for (Artifact artifact : artifacts) {
                  monitor.subTask("Modifying " + artifact.getName());
                  for (Attribute<?> attribute : artifact.getAttributes(attributeType)) {
                     Matcher matcher = pattern.matcher(attribute.toString());
                     attribute.setFromString(matcher.replaceAll(replaceText));
                  }
                  monitor.worked(1);
                  if (monitor.isCanceled()) {
                     throw new IllegalStateException("USER_PURPLE CANCELLED");
                  }
               }
               Artifacts.persistInTransaction("Attribute find replace dialog", artifacts);
               toReturn = Status.OK_STATUS;
            } catch (Exception ex) {
               if (ex.getMessage().equals("USER_PURPLE CANCELLED")) {
                  toReturn = Status.CANCEL_STATUS;
               } else {
                  toReturn = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.OK, ex.getMessage(), ex);
               }
            } finally {
               monitor.done();
            }

            return toReturn;
         }
      };
      Jobs.startJob(job);
      super.okPressed();
   }
   private static class ArtifactTypeLabelProvider implements ILabelProvider {

      @Override
      public Image getImage(Object element) {
         return null;
      }

      @Override
      public String getText(Object element) {
         if (element instanceof AttributeType) {
            return ((AttributeType) element).getName();
         } else {
            return element.toString();
         }
      }

      @Override
      public void addListener(ILabelProviderListener listener) {
         // do nothing
      }

      @Override
      public void dispose() {
         // do nothing
      }

      @Override
      public boolean isLabelProperty(Object element, String property) {
         return true;
      }

      @Override
      public void removeListener(ILabelProviderListener listener) {
         // do nothing
      }
   }
}

Back to the top