6: Add Load/Save state functionality
Task-Url: https://github.com/eyck/txviewer/issues/issue/6
This commit is contained in:
@ -0,0 +1,57 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2015 MINRES Technologies GmbH and others.
|
||||
* 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:
|
||||
* MINRES Technologies GmbH - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package com.minres.scviewer.e4.application.handlers;
|
||||
|
||||
import javax.inject.Named;
|
||||
|
||||
import org.eclipse.e4.core.di.annotations.CanExecute;
|
||||
import org.eclipse.e4.core.di.annotations.Execute;
|
||||
import org.eclipse.e4.ui.model.application.MApplication;
|
||||
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
||||
import org.eclipse.e4.ui.workbench.modeling.EModelService;
|
||||
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.eclipse.swt.widgets.FileDialog;
|
||||
import org.eclipse.swt.widgets.Shell;
|
||||
|
||||
import com.minres.scviewer.e4.application.parts.WaveformViewer;
|
||||
public class LoadStoreSettingsHandler {
|
||||
|
||||
static final String PARAMETER_ID="com.minres.scviewer.e4.application.commandparameter.loadStore";
|
||||
|
||||
@CanExecute
|
||||
public boolean canExecute(EPartService partService) {
|
||||
MPart part = partService.getActivePart();
|
||||
if(part==null) return false;
|
||||
return (part.getObject() instanceof WaveformViewer);
|
||||
}
|
||||
|
||||
@Execute
|
||||
public void execute(@Named(PARAMETER_ID) String param, Shell shell, MApplication app, EModelService modelService,
|
||||
EPartService partService){
|
||||
boolean load = "load".equals(param);
|
||||
FileDialog dialog = new FileDialog(shell, load?SWT.OPEN:SWT.SAVE);
|
||||
dialog.setFilterExtensions (new String []{"*.scview"});
|
||||
if(!load) dialog.setFileName("SCViewer.scview");
|
||||
String res = dialog.open();
|
||||
MPart part = partService.getActivePart();
|
||||
if(res!=null && part!=null){
|
||||
Object obj = part.getObject();
|
||||
if(obj instanceof WaveformViewer){
|
||||
if(load)
|
||||
((WaveformViewer)obj).loadState(res);
|
||||
else
|
||||
((WaveformViewer)obj).saveState(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -27,8 +27,9 @@ public class OpenHandler {
|
||||
|
||||
@Execute
|
||||
public void execute(Shell shell, MApplication app, EModelService modelService, EPartService partService){
|
||||
FileDialog dialog = new FileDialog(shell, SWT.MULTI);
|
||||
dialog.setFilterExtensions (new String []{"vcd", "txdb", "txlog"});
|
||||
FileDialog dialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI);
|
||||
// dialog.setFilterExtensions (new String []{"vcd", "txdb", "txlog"});
|
||||
dialog.setFilterExtensions (new String []{"*.vcd;*.txdb;*.txlog"});
|
||||
dialog.open();
|
||||
String path = dialog.getFilterPath();
|
||||
for(String fileName: dialog.getFileNames()){
|
||||
@ -36,8 +37,7 @@ public class OpenHandler {
|
||||
if(file.exists()){
|
||||
// MPart part = MBasicFactory.INSTANCE.createPart();
|
||||
// part.setLabel(fileName);
|
||||
// part.setContributionURI("bundleclass://com.minres.scviewer.e4.application/"+
|
||||
// WaveformViewerPart.class.getName());
|
||||
// part.setContributionURI("bundleclass://com.minres.scviewer.e4.application/"+ WaveformViewerPart.class.getName());
|
||||
MPart part = partService .createPart("com.minres.scviewer.e4.application.partdescriptor.waveformviewer");
|
||||
part.setLabel(file.getName());
|
||||
|
||||
|
@ -13,12 +13,17 @@ package com.minres.scviewer.e4.application.parts;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Properties;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ -426,6 +431,40 @@ public class WaveformViewer implements IFileChangeListener, IPreferenceChangeLis
|
||||
saveWaveformViewerState(persistedState);
|
||||
}
|
||||
|
||||
public void saveState(String fileName){
|
||||
Map<String, String> persistedState = new HashMap<>();
|
||||
persistedState.put(DATABASE_FILE + "S", Integer.toString(filesToLoad.size()));
|
||||
Integer index = 0;
|
||||
for (File file : filesToLoad) {
|
||||
persistedState.put(DATABASE_FILE + index, file.getAbsolutePath());
|
||||
index++;
|
||||
}
|
||||
saveWaveformViewerState(persistedState);
|
||||
Properties props = new Properties();
|
||||
props.putAll(persistedState);
|
||||
try {
|
||||
FileOutputStream out = new FileOutputStream(fileName);
|
||||
props.store(out, "Written by SCViewer");
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void loadState(String fileName){
|
||||
Properties props = new Properties();
|
||||
try {
|
||||
FileInputStream in = new FileInputStream(fileName);
|
||||
props.load(in);
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
HashMap<String, String> propMap = new HashMap<String, String>((Map) props);
|
||||
restoreWaveformViewerState(propMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save waveform viewer state.
|
||||
*
|
||||
|
Reference in New Issue
Block a user