]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/handler/ConfigureIssueSources.java
Externalize strings
[simantics/platform.git] / bundles / org.simantics.issues.ui / src / org / simantics / issues / ui / handler / ConfigureIssueSources.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.issues.ui.handler;
13
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17
18 import org.eclipse.core.commands.AbstractHandler;
19 import org.eclipse.core.commands.ExecutionEvent;
20 import org.eclipse.core.commands.ExecutionException;
21 import org.eclipse.jface.dialogs.Dialog;
22 import org.eclipse.jface.viewers.CheckStateChangedEvent;
23 import org.eclipse.jface.viewers.CheckboxTableViewer;
24 import org.eclipse.jface.viewers.ICheckStateListener;
25 import org.eclipse.jface.viewers.ICheckStateProvider;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.events.SelectionListener;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.swt.widgets.Table;
32 import org.eclipse.ui.PlatformUI;
33 import org.simantics.Simantics;
34 import org.simantics.db.ReadGraph;
35 import org.simantics.db.Resource;
36 import org.simantics.db.WriteGraph;
37 import org.simantics.db.common.NamedResource;
38 import org.simantics.db.common.request.PossibleObjectWithType;
39 import org.simantics.db.common.request.UniqueRead;
40 import org.simantics.db.common.request.WriteRequest;
41 import org.simantics.db.common.utils.NameUtils;
42 import org.simantics.db.exception.DatabaseException;
43 import org.simantics.db.layer0.request.PossibleActiveModel;
44 import org.simantics.db.layer0.util.RemoverUtil;
45 import org.simantics.issues.common.IssueUtils;
46 import org.simantics.issues.ontology.IssueResource;
47 import org.simantics.layer0.Layer0;
48 import org.simantics.modeling.ModelingUtils;
49 import org.simantics.utils.ui.ErrorLogger;
50 import org.simantics.utils.ui.dialogs.ListDialog;
51
52 /**
53  * @author Antti Villberg
54  */
55 public class ConfigureIssueSources extends AbstractHandler {
56
57         private static class IssueSourceEntry extends NamedResource {
58                 private boolean checked;
59                 public IssueSourceEntry(String name, Resource resource, boolean checked) {
60                         super(name, resource);
61                         this.checked = checked;
62                 }
63                 public boolean isChecked() {
64                         return checked;
65                 }
66                 public void setChecked(boolean value) {
67                         checked = value;
68                 }
69         }
70         
71     @Override
72     public Object execute(ExecutionEvent event) throws ExecutionException {
73
74       try {
75
76         final List<IssueSourceEntry> sources = Simantics.getSession().syncRequest(new UniqueRead<List<IssueSourceEntry>>() {
77
78                         @Override
79                         public List<IssueSourceEntry> perform(ReadGraph graph) throws DatabaseException {
80                                 
81                                 Resource activeModel = graph.syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));
82                                 if(activeModel == null) return Collections.emptyList();
83                                 
84                                 List<IssueSourceEntry> result = new ArrayList<IssueSourceEntry>();
85                                 Layer0 L0 = Layer0.getInstance(graph);
86                                 IssueResource ISSUE = IssueResource.getInstance(graph);
87                                 for(Resource type : ModelingUtils.searchByType(graph, activeModel, ISSUE.IssueSourceType)) {
88                                         String name = NameUtils.getSafeLabel(graph, type);
89                                         boolean exists = graph.syncRequest(new PossibleObjectWithType(activeModel, L0.ConsistsOf, type)) != null;
90                                         boolean deprecated = graph.hasStatement(type, L0.Deprecated);
91                                         if(!exists && deprecated) continue;
92                                         result.add(new IssueSourceEntry(name, type, exists));
93                                 }
94                                 return result;
95                         }
96                 
97         });
98         
99          Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
100          ListDialog<IssueSourceEntry> dialog = new ListDialog<IssueSourceEntry>(
101                  shell, sources,
102                  Messages.ConfigureIssueSources_SelectAvailableIssueSources,
103                  Messages.ConfigureIssueSources_SelectedSourcesAddRemoveMsg) {
104                  
105                     protected CheckboxTableViewer createViewer(Composite composite) {
106                         CheckboxTableViewer viewer = CheckboxTableViewer.newCheckList(
107                                 composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
108                         final Table table = (Table)viewer.getControl();
109                         viewer.setCheckStateProvider(new ICheckStateProvider() {
110                                                 
111                                                 @Override
112                                                 public boolean isGrayed(Object arg0) {
113                                                         return false;
114                                                 }
115                                                 
116                                                 @Override
117                                                 public boolean isChecked(Object arg0) {
118                                                         IssueSourceEntry entry = (IssueSourceEntry)arg0;
119                                                         return entry.isChecked();
120                                                 }
121                                                 
122                                         });
123                         viewer.addCheckStateListener(new ICheckStateListener() {
124                                                 
125                                                 @Override
126                                                 public void checkStateChanged(CheckStateChangedEvent arg0) {
127                                                         IssueSourceEntry entry = (IssueSourceEntry)arg0.getElement();
128                                                         entry.setChecked(arg0.getChecked());
129                                                 }
130                                         });
131                         table.addSelectionListener(new SelectionListener () {
132                                 @Override
133                                 public void widgetSelected(SelectionEvent e) {
134                                         table.deselectAll();
135                                 }
136                                 @Override
137                                 public void widgetDefaultSelected(SelectionEvent e) {}
138                         });
139                         return viewer;
140                     }
141
142          };
143          int result = dialog.open();
144          if (result != Dialog.OK)
145              return null;
146         
147         Simantics.getSession().syncRequest(new WriteRequest() {
148
149                         @Override
150                         public void perform(WriteGraph graph) throws DatabaseException {
151                                 
152                                 Resource activeModel = graph.syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));
153                                 if(activeModel == null) return;
154                                 
155                                 Layer0 L0 = Layer0.getInstance(graph);
156
157                                 for(IssueSourceEntry entry : sources) {
158
159                                         Resource existing = graph.syncRequest(new PossibleObjectWithType(activeModel, L0.ConsistsOf, entry.getResource())); 
160                                         
161                                         if(existing == null && entry.isChecked()) {
162                                                 String name = NameUtils.getSafeLabel(graph, entry.getResource());
163                                                 IssueUtils.addIssueSource(graph, activeModel, entry.getResource(), name);
164                                         }
165                                         
166                                         if(existing != null && !entry.isChecked()) {
167                                                 RemoverUtil.remove(graph, existing);
168                                         }
169                                         
170                                 }
171                                 
172                         }
173                 
174         });
175          
176 //        try {
177 //            PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
178 //                @Override
179 //                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
180 //                    try {
181 //                        purgeResolvedIssues(monitor);
182 //                    } catch (DatabaseException e) {
183 //                        throw new InvocationTargetException(e);
184 //                    } finally {
185 //                        monitor.done();
186 //                    }
187 //                }
188 //            });
189 //        } catch (InvocationTargetException e) {
190 //            ErrorLogger.defaultLogError(e);
191         } catch (DatabaseException e) {
192             ErrorLogger.defaultLogError(e);
193         }
194         return null;
195     }
196
197 //    private void purgeResolvedIssues(IProgressMonitor monitor) throws DatabaseException {
198 //        Session session = Simantics.getSession();
199 //        final Resource project = Simantics.getProjectResource();
200 //        if (project == null)
201 //            return;
202 //
203 //        final SubMonitor mon = SubMonitor.convert(monitor, "Purging resolved issues...", 100);
204 //
205 //        session.syncRequest(new DelayedWriteRequest() {
206 //            @Override
207 //            public void perform(WriteGraph graph) throws DatabaseException {
208 //                graph.markUndoPoint();
209 //                IssueResource ISSUE = IssueResource.getInstance(graph);
210 //                Set<Resource> toBeRemoved = new HashSet<Resource>();
211 //                Map<Resource, Boolean> sourceIsContinuous = new THashMap<Resource, Boolean>(); 
212 //                for (Resource activeIssue : graph.syncRequest(new AllActiveIssues(project))) {
213 //                    if (graph.hasStatement(activeIssue, ISSUE.Resolved)) {
214 //                        Resource managedBy = graph.getPossibleObject(activeIssue, ISSUE.IssueSource_Manages_Inverse);
215 //                        if (managedBy != null) {
216 //                            Boolean isContinuous = sourceIsContinuous.get(managedBy);
217 //                            if (isContinuous == null) {
218 //                                isContinuous = graph.isInstanceOf(managedBy, ISSUE.ContinuousIssueSource);
219 //                                sourceIsContinuous.put(managedBy, isContinuous);
220 //                            }
221 //                            if (isContinuous)
222 //                                continue;
223 //                        }
224 //                        toBeRemoved.add(activeIssue);
225 //                    }
226 //                }
227 //
228 //                mon.setTaskName("Purging " + toBeRemoved.size() + " resolved batch issues...");
229 //                mon.setWorkRemaining(toBeRemoved.size());
230 //                StringBuilder sb = new StringBuilder();
231 //                sb.append("Purged " + toBeRemoved.size() + " resolved batch issue(s)");
232 //                for (Resource remove : toBeRemoved) {
233 //                    //sb.append(NameUtils.getSafeLabel(graph, remove) + " ");
234 //                    RemoverUtil.remove(graph, remove);
235 //                    mon.worked(1);
236 //                }
237 //                Layer0Utils.addCommentMetadata(graph, sb.toString());
238 //            }
239 //        });
240 //    }
241
242 }