+ }
+
+ private void initializeData() {
+ try {
+ getContainer().run(true, true, monitor -> {
+ try {
+ initializeModelData(monitor);
+ SWTUtils.asyncExec(model, () -> {
+ if (!model.isDisposed())
+ initializeModelAndItemSelection();
+ });
+ } catch (DatabaseException e) {
+ throw new InvocationTargetException(e);
+ } finally {
+ monitor.done();
+ }
+ });
+ } catch (InvocationTargetException e) {
+ setErrorMessage(e.getMessage());
+ LOGGER.error("Failed to initialized model data for wizard.", e.getCause());
+ } catch (InterruptedException e) {
+ setErrorMessage(e.getMessage());
+ LOGGER.error("Interrupted wizard model data initialization.", e);
+ }
+ }
+
+ private void initializeModelData(IProgressMonitor monitor) throws DatabaseException {
+ models = exportModel.sessionContext.getSession().syncRequest(
+ (Read<List<Model>>) graph -> readModelData(monitor, graph));
+ }
+
+ private List<Model> readModelData(IProgressMonitor monitor, ReadGraph graph) throws DatabaseException {
+ List<Model> result = new ArrayList<>();
+ Layer0 L0 = Layer0.getInstance(graph);
+ Collection<Resource> models = graph.syncRequest(new ProjectModels(Simantics.getProjectResource()));
+ SubMonitor mon = SubMonitor.convert(monitor, "Reading model subscriptions", models.size());
+ for (Resource model : models) {
+ String name = graph.getPossibleRelatedValue(model, L0.HasName, Bindings.STRING);
+ if (name != null) {
+ name = NameLabelUtil.modalName(graph, model);
+ mon.subTask(name);
+ Model m = new Model(new NamedResource(name, model));
+ m.initialize(graph, initialSelection);
+ result.add(m);
+ }
+ mon.worked(1);
+ }
+ return result;
+ }
+
+ private void initializeModelAndItemSelection() {
+ boolean initialSelectionDone = false;
+ for (int i = 0; i < models.size(); i++) {
+ Model m = models.get(i);
+ model.add(m.model.getName());
+ if (!initialSelectionDone) {
+ boolean hasInitialSelection = m.sortedSubs.stream().anyMatch(m.initiallySelectedSubscriptions::contains);
+ if (hasInitialSelection) {
+ initializeItemSelectionForModel(m);
+ initialSelectionDone = true;
+ }
+ }
+ }
+ }
+
+ private void initializeItemSelectionForModel(Model m) {
+ int i = models.indexOf(m);
+ model.select(i);
+ item.removeAll();
+ exportModel.items.clear();
+ int index = 0;
+ int firstIndex = -1;
+ item.setRedraw(false);
+ for (NamedResource nr : m.sortedSubs) {
+ TableItem ti = new TableItem(item, SWT.NONE);
+ ti.setText(nr.getName());
+ ti.setData(nr);
+ if (m.initiallySelectedSubscriptions.contains(nr)) {
+ exportModel.items.add(nr.getResource());
+ ti.setChecked(true);
+ if (firstIndex == -1)
+ firstIndex = index;
+ }
+ index++;
+ }
+ item.setTopIndex(Math.max(0, firstIndex));
+ item.setData(m);
+ item.setRedraw(true);
+
+ int checked = countCheckedItems(item);
+ updateSelectAll(checked > 0, checked < item.getItemCount(), false);
+ }
+
+ private void updateSelectAll(boolean checked, boolean gray, boolean notify) {
+ if (checked) {
+ selectAllItems.setText("Select None");
+ } else {
+ selectAllItems.setText("Select All");
+ }
+ if (!notify)
+ selectAllItems.removeSelectionListener(selectAllItemsListener);
+ selectAllItems.setGrayed(checked && gray);
+ selectAllItems.setSelection(checked);
+ if (!notify)
+ selectAllItems.addSelectionListener(selectAllItemsListener);
+ }
+
+ protected int countCheckedItems(Table table) {
+ int ret = 0;
+ for (TableItem item : table.getItems())
+ ret += item.getChecked() ? 1 : 0;
+ return ret;