Java tutorial
/************************************************************************************************* * Copyright (c) 2013, Lex @ le boxon de Lex: http://le-boxon-de-lex.fr * * This program is free software; you can redistribute it and/or modify it under the terms * of the GNU General Public License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with this program; * if not, see <http://www.gnu.org/licenses>. * * Additional permission under GNU GPL version 3 section 7 * * If you modify this Program, or any covered work, by linking or combining it with Eclipse (or * a modified version of that library), containing parts covered by the terms of Eclipse Public * License (EPL), the licensors of this Program grant you additional permission to convey the * resulting work. Corresponding Source for a non-source form of such a combination shall include * the source code for the parts of D'Aplomb used as well as that of the covered work. ***************************************************************************************************/ package net.leboxondelex.daplomb.ui.dialogs; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Locale; import net.leboxondelex.daplomb.managers.PreferencesManager; import net.leboxondelex.daplomb.translations.Messages; import net.leboxondelex.daplomb.utils.LogUtils; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.TitleAreaDialog; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.viewers.ArrayContentProvider; import org.eclipse.jface.viewers.ComboViewer; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.DirectoryDialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; /** * @author Lex */ public class PreferencesDialog extends TitleAreaDialog { private boolean langHasChanged = false; /** * Constructor. * @param parentShell */ public PreferencesDialog(Shell parentShell) { super(parentShell); } /* * (non-Javadoc) * @see org.eclipse.jface.window.Window * #getShellStyle() */ @Override protected int getShellStyle() { return super.getShellStyle() | SWT.RESIZE; } /** * @return the langHasChanged */ public boolean langHasChanged() { return this.langHasChanged; } /* * (non-Javadoc) * @see org.eclipse.jface.dialogs.TitleAreaDialog * #createDialogArea(org.eclipse.swt.widgets.Composite) */ @Override protected Control createDialogArea(Composite parent) { Composite bigContainer = (Composite) super.createDialogArea(parent); GridLayoutFactory.swtDefaults().margins(0, 0).applyTo(bigContainer); bigContainer.setLayoutData(new GridData(GridData.FILL_BOTH)); Composite container = new Composite(bigContainer, SWT.NONE); GridLayoutFactory.swtDefaults().margins(10, 10).numColumns(2).applyTo(container); container.setLayoutData(new GridData(GridData.FILL_BOTH)); bigContainer.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); container.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); parent.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); // Library root Label l = new Label(container, SWT.NONE); l.setText(Messages.getString("ShowPreferencesHandler.0")); //$NON-NLS-1$ l.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); Text libraryText = createDirectoryBrowser(container, PreferencesManager.INSTANCE.getLibraryRootDirectory(), Messages.getString("ShowPreferencesHandler.1"), //$NON-NLS-1$ Messages.getString("ShowPreferencesHandler.2")); //$NON-NLS-1$ libraryText.addListener(SWT.Modify, new Listener() { @Override public void handleEvent(Event event) { String s = ((Text) event.widget).getText(); PreferencesManager.INSTANCE.saveLibraryRootDirectory(s); } }); // Library's favorite folder l = new Label(container, SWT.NONE); l.setText(Messages.getString("ShowPreferencesHandler.3")); //$NON-NLS-1$ l.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); Text defaultFolderText = createDirectoryBrowser(container, PreferencesManager.INSTANCE.getLibraryPreferredDirectory(), Messages.getString("ShowPreferencesHandler.4"), //$NON-NLS-1$ Messages.getString("ShowPreferencesHandler.5")); //$NON-NLS-1$ defaultFolderText.addListener(SWT.Modify, new Listener() { @Override public void handleEvent(Event event) { String s = ((Text) event.widget).getText(); PreferencesManager.INSTANCE.saveLibraryPreferredDirectory(s); } }); // FF MPEG location l = new Label(container, SWT.NONE); l.setText("FFmpeg :"); l.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); boolean win = System.getProperty("os.name").toLowerCase().contains("win"); String s = PreferencesManager.INSTANCE.getFfMpegLocation(); File f = s != null ? new File(s) : null; Text ffMpegLocationText = createFileBrowser(container, f, "Slectionnez l'emplacement d'un excutable FFmpeg.", win ? "*.exe" : "*.*", "FFmpeg"); ffMpegLocationText.addListener(SWT.Modify, new Listener() { @Override public void handleEvent(Event event) { String s = ((Text) event.widget).getText(); PreferencesManager.INSTANCE.saveFfMpegLocation(s); } }); // Language l = new Label(container, SWT.NONE); l.setText("Langage :"); l.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); final ComboViewer langViewer = new ComboViewer(container, SWT.READ_ONLY | SWT.BORDER | SWT.DROP_DOWN); langViewer.setContentProvider(new ArrayContentProvider()); langViewer.setLabelProvider(new LabelProvider() { @Override public String getText(Object element) { return ((Locale) element).getDisplayName(); } }); List<Locale> locales = new ArrayList<Locale>(); locales.add(Locale.FRENCH); locales.add(Locale.ENGLISH); langViewer.setInput(locales); Locale currentLocale = PreferencesManager.INSTANCE.getApplicationLanguage(); if (currentLocale == null) currentLocale = locales.get(0); langViewer.setSelection(new StructuredSelection(currentLocale)); langViewer.getCombo().addListener(SWT.Selection, new Listener() { @Override public void handleEvent(Event event) { Object o = ((IStructuredSelection) langViewer.getSelection()).getFirstElement(); PreferencesManager.INSTANCE.saveApplicationLanguage((Locale) o); Messages.switchLocale((Locale) o); PreferencesDialog.this.langHasChanged = true; } }); // Debug Button debugButton = new Button(container, SWT.CHECK); debugButton.setText("Activer les traces de DEBUG"); debugButton.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); debugButton.setSelection(PreferencesManager.INSTANCE.isApplicationDebug()); GridDataFactory.swtDefaults().span(2, 1).indent(0, 20).applyTo(debugButton); debugButton.addListener(SWT.Selection, new Listener() { @Override public void handleEvent(Event e) { boolean selection = ((Button) e.widget).getSelection(); PreferencesManager.INSTANCE.saveApplicationDebug(selection); LogUtils.updateLogLevel(); } }); // Shell properties setTitle(Messages.getString("ShowPreferencesHandler.6")); //$NON-NLS-1$ setMessage(Messages.getString("ShowPreferencesHandler.7")); //$NON-NLS-1$ return bigContainer; } /* * (non-Javadoc) * @see org.eclipse.jface.dialogs.Dialog * #createButtonsForButtonBar(org.eclipse.swt.widgets.Composite) */ @Override protected void createButtonsForButtonBar(Composite parent) { parent.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); parent.getParent().setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); } /** * Creates a directory browser. * @param parent * @param originalLocation * @param title * @param msg * @return the text field */ private static Text createDirectoryBrowser(final Composite parent, final File originalLocation, final String title, final String msg) { Composite browserComposite = new Composite(parent, SWT.NONE); GridLayoutFactory.swtDefaults().margins(0, 0).numColumns(2).applyTo(browserComposite); browserComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); browserComposite.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); final Text text = new Text(browserComposite, SWT.SINGLE | SWT.READ_ONLY | SWT.BORDER); text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); text.setText(originalLocation.getAbsolutePath()); Button browseButton = new Button(browserComposite, SWT.PUSH); browseButton.setText(Messages.getString("ShowPreferencesHandler.8")); //$NON-NLS-1$ browseButton.addListener(SWT.Selection, new Listener() { @Override public void handleEvent(Event event) { DirectoryDialog dlg = new DirectoryDialog(parent.getShell()); dlg.setMessage(msg); dlg.setText(title); if (originalLocation != null) dlg.setFilterPath(originalLocation.getAbsolutePath()); String path = dlg.open(); if (path != null) text.setText(path); } }); return text; } /** * Creates a directory browser. * @param parent * @param originalLocation * @param title * @param msg * @return the text field */ private static Text createFileBrowser(final Composite parent, final File originalLocation, final String msg, final String extension, final String extensionDesc) { Composite browserComposite = new Composite(parent, SWT.NONE); GridLayoutFactory.swtDefaults().margins(0, 0).numColumns(2).applyTo(browserComposite); browserComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); browserComposite.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); final Text text = new Text(browserComposite, SWT.SINGLE | SWT.BORDER); text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); if (originalLocation != null) text.setText(originalLocation.getAbsolutePath()); Button browseButton = new Button(browserComposite, SWT.PUSH); browseButton.setText(Messages.getString("ShowPreferencesHandler.8")); //$NON-NLS-1$ browseButton.addListener(SWT.Selection, new Listener() { @Override public void handleEvent(Event event) { FileDialog dlg = new FileDialog(parent.getShell(), SWT.SINGLE); dlg.setFilterExtensions(new String[] { extension }); dlg.setFilterNames(new String[] { extensionDesc }); dlg.setFilterPath(originalLocation.getAbsolutePath()); dlg.setText(msg); if (dlg.open() != null) text.setText(new File(dlg.getFilterPath(), dlg.getFileName()).getAbsolutePath()); } }); return text; } }