use of org.eclipse.swt.widgets.FileDialog in project translationstudio8 by heartsome.
the class XLIFFEditorImplWithNatTable method performSaveAs.
/**
* 执行另存为
* @param progressMonitor
* 进度条;
*/
private void performSaveAs(IProgressMonitor progressMonitor) {
Shell shell = getSite().getShell();
final IEditorInput input = getEditorInput();
// 新的EditorInput
final IEditorInput newInput;
// 原始的file
final File oldFile;
// if (input instanceof IURIEditorInput && !(input instanceof IFileEditorInput)) { // 外部文件
FileDialog dialog = new FileDialog(shell, SWT.SAVE);
URI uri = ((IURIEditorInput) input).getURI();
IPath oldPath = URIUtil.toPath(uri);
if (oldPath != null) {
dialog.setFileName(oldPath.lastSegment());
// 设置所在文件夹
dialog.setFilterPath(oldPath.removeLastSegments(1).toOSString());
oldFile = oldPath.toFile();
} else {
oldFile = new File(uri);
}
// 得到保存路径
String newPath = dialog.open();
if (newPath == null) {
if (progressMonitor != null)
progressMonitor.setCanceled(true);
return;
}
// 检查文件是否存在,如果存在则确认是否覆盖
final File localFile = new File(newPath);
if (localFile.exists()) {
String msg = MessageFormat.format(Messages.getString("editor.XLIFFEditorImplWithNatTable.msg1"), newPath);
MessageDialog overwriteDialog = new MessageDialog(shell, Messages.getString("editor.XLIFFEditorImplWithNatTable.overwriteDialog"), null, msg, MessageDialog.WARNING, new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL }, 1);
if (overwriteDialog.open() != MessageDialog.OK) {
if (progressMonitor != null) {
progressMonitor.setCanceled(true);
return;
}
}
}
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
// 得到新文件
IFile file = root.getFileForLocation(URIUtil.toPath(localFile.toURI()));
if (file != null) {
// 是“WorkSpace”内的文件
newInput = new FileEditorInput(file);
} else {
// 不是“WorkSpace”内的文件
try {
IFileStore fileStore = EFS.getStore(localFile.toURI());
newInput = new FileStoreEditorInput(fileStore);
} catch (CoreException ex) {
// EditorsPlugin.log(ex.getStatus());
LOGGER.error("", ex);
String title = Messages.getString("editor.XLIFFEditorImplWithNatTable.msgTitle1");
String msg = MessageFormat.format(Messages.getString("editor.XLIFFEditorImplWithNatTable.msg2"), ex.getMessage());
MessageDialog.openError(shell, title, msg);
return;
}
}
// } else {
// SaveAsDialog dialog = new SaveAsDialog(shell);
// // 源文件
// IFile original = (input instanceof IFileEditorInput) ? ((IFileEditorInput) input).getFile() : null;
// if (original != null) {
// dialog.setOriginalFile(original); // 添加源文件信息
// oldFile = original.getLocation().toFile();
// if ((!oldFile.exists() || !oldFile.canRead()) && original != null) {
// String message = MessageFormat.format(
// "The original file ''{0}'' has been deleted or is not accessible.", original.getName());
// dialog.setErrorMessage(null);
// dialog.setMessage(message, IMessageProvider.WARNING);
// }
// } else {
// oldFile = null;
// }
// dialog.create();
//
// if (dialog.open() == MessageDialog.CANCEL) { // 打开“另存为”对话框,用户点击了“取消”按钮
// if (progressMonitor != null)
// progressMonitor.setCanceled(true);
// return;
// }
//
// IPath filePath = dialog.getResult(); // 获得用户选择的路径
// if (filePath == null) { // 检查路径
// if (progressMonitor != null)
// progressMonitor.setCanceled(true);
// return;
// }
//
// IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
// IFile file = root.getFile(filePath);
// newInput = new FileEditorInput(file);
// }
saveAs(newInput, oldFile, progressMonitor);
}
use of org.eclipse.swt.widgets.FileDialog in project translationstudio8 by heartsome.
the class ExportAsHtmlHandler method execute.
public Object execute(ExecutionEvent event) throws ExecutionException {
String elementName = event.getParameter("elementName");
IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
Shell shell = activeEditor.getEditorSite().getShell();
if (activeEditor == null || !(activeEditor instanceof XLIFFEditorImplWithNatTable)) {
return null;
}
XLIFFEditorImplWithNatTable xliffEditor = (XLIFFEditorImplWithNatTable) activeEditor;
if (xliffEditor.isMultiFile()) {
MessageDialog.openInformation(shell, "", "当前编辑器打开了多个文件,无法执行该操作。");
}
IEditorInput input = xliffEditor.getEditorInput();
URI uri = null;
if (input instanceof FileEditorInput) {
uri = ((FileEditorInput) input).getURI();
} else if (input instanceof FileStoreEditorInput) {
uri = ((FileStoreEditorInput) input).getURI();
} else {
return null;
}
File xliff = new File(uri);
FileDialog fd = new FileDialog(shell, SWT.SAVE);
String[] names = { "HTML Files [*.html]", "All Files [*.*]" };
//$NON-NLS-1$ //$NON-NLS-2$
String[] extensions = { "*.html", "*.*" };
fd.setFilterExtensions(extensions);
fd.setFilterNames(names);
//$NON-NLS-1$
fd.setFileName(xliff.getName() + ".html");
String out = fd.open();
if (out == null) {
return null;
}
XLFHandler handler = xliffEditor.getXLFHandler();
boolean result = handler.saveAsHtml(xliff.getAbsolutePath(), out, elementName);
if (result) {
IWorkbenchPage page = xliffEditor.getEditorSite().getPage();
OpenEditorUtil.OpenFileWithSystemEditor(page, out);
} else {
MessageDialog.openInformation(shell, "", "文件 “" + out + "” 保存失败!请重试。");
}
return null;
}
use of org.eclipse.swt.widgets.FileDialog in project translationstudio8 by heartsome.
the class AddOrUpdateLanguageDialog method createDialogArea.
@Override
protected Control createDialogArea(Composite parent) {
Composite tparent = (Composite) super.createDialogArea(parent);
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
tparent.setLayoutData(data);
GridLayout layout = new GridLayout(3, false);
tparent.setLayout(layout);
GridData txtData = new GridData(GridData.FILL_HORIZONTAL);
txtData.horizontalSpan = 2;
Label lbl = new Label(tparent, SWT.NONE);
lbl.setText(Messages.getString("languagecode.AddOrUpdateLanguageDialog.lblLangImage"));
GridDataFactory.swtDefaults().align(SWT.RIGHT, SWT.CENTER).applyTo(lbl);
imageLabel = new Label(tparent, SWT.NONE);
GridData imGd = new GridData();
imGd.widthHint = 16;
imGd.heightHint = 12;
imageLabel.setLayoutData(imGd);
imageLabel.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
try {
String bundlePath = FileLocator.toFileURL(Activator.getDefault().getBundle().getEntry("")).getPath();
if (this.imagePath != null && !this.imagePath.equals("")) {
String imagePath = bundlePath + this.imagePath;
if (image != null && !image.isDisposed()) {
image.dispose();
}
image = new Image(getShell().getDisplay(), imagePath);
}
if (image != null) {
ImageData imgData = image.getImageData().scaledTo(16, 12);
if (image != null && !image.isDisposed()) {
image.dispose();
}
image = new Image(getShell().getDisplay(), imgData);
imageLabel.setData(this.imagePath);
} else {
if (image != null && !image.isDisposed()) {
image.dispose();
}
image = new Image(getShell().getDisplay(), bundlePath + ImageConstant.LANG_EMPTYPIC);
}
imageLabel.setImage(image);
} catch (IOException e) {
e.printStackTrace();
}
imageLabel.setToolTipText(Messages.getString("languagecode.AddOrUpdateLanguageDialog.imageLabel"));
imageLabel.addListener(SWT.MouseUp, new Listener() {
public void handleEvent(Event event) {
FileDialog dlg = new FileDialog(getShell());
dlg.setFilterExtensions(new String[] { "*.png" });
String path = dlg.open();
if (path != null) {
ImageData data = new ImageData(path).scaledTo(16, 12);
if (image != null && !image.isDisposed()) {
image.dispose();
}
image = new Image(getShell().getDisplay(), data);
imageLabel.setImage(image);
imageLabel.setData(path);
}
}
});
new Label(tparent, SWT.NONE).setText(Messages.getString("languagecode.AddOrUpdateLanguageDialog.lblImage"));
lbl = new Label(tparent, SWT.NONE);
lbl.setText(Messages.getString("languagecode.AddOrUpdateLanguageDialog.txtCode"));
GridDataFactory.swtDefaults().align(SWT.RIGHT, SWT.CENTER).applyTo(lbl);
txtCode = new Text(tparent, SWT.BORDER);
txtCode.setLayoutData(txtData);
txtCode.setText(strCode == null ? "" : strCode);
lbl = new Label(tparent, SWT.NONE);
lbl.setText(Messages.getString("languagecode.AddOrUpdateLanguageDialog.txtName"));
GridDataFactory.swtDefaults().align(SWT.RIGHT, SWT.CENTER).applyTo(lbl);
txtName = new Text(tparent, SWT.BORDER);
txtName.setLayoutData(txtData);
txtName.setText(strName == null ? "" : strName);
lbl = new Label(tparent, SWT.NONE);
lbl.setText(Messages.getString("languagecode.AddOrUpdateLanguageDialog.isBidi"));
GridDataFactory.swtDefaults().align(SWT.RIGHT, SWT.CENTER).applyTo(lbl);
btnIsBidi = new Button(tparent, SWT.RADIO);
btnIsBidi.setText(Messages.getString("languagecode.AddOrUpdateLanguageDialog.btnIsBidi"));
btnIsNotBidi = new Button(tparent, SWT.RADIO);
btnIsNotBidi.setText(Messages.getString("languagecode.AddOrUpdateLanguageDialog.btnIsNotBidi"));
if (blnIsBidi) {
btnIsBidi.setSelection(true);
} else {
btnIsNotBidi.setSelection(true);
}
tparent.computeSize(SWT.DEFAULT, SWT.DEFAULT);
txtCode.forceFocus();
txtCode.selectAll();
return tparent;
}
use of org.eclipse.swt.widgets.FileDialog in project translationstudio8 by heartsome.
the class NewProjectWizardSourceFilePage method createControl.
/**
* Create contents of the wizard.
* @param parent
*/
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NULL);
container.setLayout(new GridLayout(1, false));
setControl(container);
fileListViewer = new ListViewer(container, SWT.BORDER | SWT.V_SCROLL | SWT.MULTI);
fileListViewer.getList().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
fileListViewer.setContentProvider(new ArrayContentProvider());
fileListViewer.setInput(srcFileList);
if (this.converterCaller != null) {
final Button btnConvert = new Button(container, SWT.CHECK);
btnConvert.setText(Messages.getString("wizard.NewProjectWizardSourceFilePage.btnConvert"));
btnConvert.setSelection(true);
isOpenConverter = true;
btnConvert.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
isOpenConverter = btnConvert.getSelection();
}
});
}
Composite composite = new Composite(container, SWT.NONE);
composite.setLayout(new GridLayout(2, false));
composite.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, false, 1, 1));
Button addBtn = new Button(composite, SWT.NONE);
addBtn.setText(Messages.getString("wizard.NewProjectWizardSourceFilePage.addBtn"));
addBtn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
FileDialog dlg = new FileDialog(getShell(), SWT.MULTI);
// CONVERTEREXTENTION
String[] supExtentions = new String[] { "*.mif;*.idml;*.inx;*.xlf;*.rtf;*.po;*.properties;*.js;*.mqxlz;*.doc;*.xls;*.ppt;" + "*.docx;*.xlsx;*.pptx;*.odt;*.ods;*.odp;*.odg;*.rtf;*.sdlxliff;*.ttx;*.htm;*.html;*.txt;*.resx;*.rc;*.xml;*.txml", "*.*" };
dlg.setFilterExtensions(supExtentions);
if (dlg.open() != null) {
String[] files = dlg.getFileNames();
for (int i = 0; i < files.length; i++) {
StringBuffer buf = new StringBuffer(dlg.getFilterPath());
buf.append(File.separator);
buf.append(files[i]);
String file = buf.toString();
if (!srcFileList.contains(file)) {
srcFileList.add(file);
}
}
fileListViewer.refresh();
}
}
});
Button deleteBtn = new Button(composite, SWT.NONE);
deleteBtn.setText(Messages.getString("wizard.NewProjectWizardSourceFilePage.deleteBtn"));
deleteBtn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
IStructuredSelection selections = (IStructuredSelection) fileListViewer.getSelection();
Iterator<?> it = selections.iterator();
while (it.hasNext()) {
String file = (String) it.next();
srcFileList.remove(file);
}
fileListViewer.refresh();
}
});
}
use of org.eclipse.swt.widgets.FileDialog in project translationstudio8 by heartsome.
the class KeyController2 method exportCSV.
public void exportCSV(Shell shell) {
final FileDialog fileDialog = new FileDialog(shell, SWT.SAVE | SWT.SHEET);
//$NON-NLS-1$
fileDialog.setFilterExtensions(new String[] { "*.csv" });
//$NON-NLS-1$
fileDialog.setFilterNames(new String[] { Util.translateString(RESOURCE_BUNDLE, "csvFilterName") });
fileDialog.setOverwrite(true);
final String filePath = fileDialog.open();
if (filePath == null) {
return;
}
final SafeRunnable runnable = new SafeRunnable() {
public final void run() throws IOException {
Writer fileWriter = null;
try {
//$NON-NLS-1$
fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8"));
final Object[] bindingElements = bindingModel.getBindings().toArray();
for (int i = 0; i < bindingElements.length; i++) {
final BindingElement be = (BindingElement) bindingElements[i];
if (be.getTrigger() == null || be.getTrigger().isEmpty() || be.getContext() == null || be.getContext().getName() == null) {
continue;
}
StringBuffer buffer = new StringBuffer();
buffer.append(ESCAPED_QUOTE + Util.replaceAll(be.getCategory(), ESCAPED_QUOTE, REPLACEMENT) + ESCAPED_QUOTE + DELIMITER);
buffer.append(ESCAPED_QUOTE + be.getName() + ESCAPED_QUOTE + DELIMITER);
buffer.append(ESCAPED_QUOTE + be.getTrigger().format() + ESCAPED_QUOTE + DELIMITER);
buffer.append(ESCAPED_QUOTE + be.getContext().getName() + ESCAPED_QUOTE);
//$NON-NLS-1$
buffer.append(System.getProperty("line.separator"));
fileWriter.write(buffer.toString());
}
} finally {
if (fileWriter != null) {
try {
fileWriter.close();
} catch (final IOException e) {
// At least I tried.
}
}
}
}
};
SafeRunner.run(runnable);
}
Aggregations