Package de.innovationgate.eclipse.dialogs

Source Code of de.innovationgate.eclipse.dialogs.LabelingDialog

/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* 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:
*     Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.dialogs;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import java.util.Map.Entry;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.StatusDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.Preferences;
import de.innovationgate.eclipse.editors.helpers.LabelTagReplaceHandler;
import de.innovationgate.eclipse.utils.Activator;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;

public class LabelingDialog extends StatusDialog implements ModifyListener, SelectionListener {

  // private static final String LABELSFOLDERDE = "labels_de";
  // private static final String FILESPATH = "files";
  // private static final String GENERALLABELFILE = "/general.properties";


  private Properties _currentLabelContent = null;
  private ITextSelection _textSelection;
  private IDocument _doc;
  private Combo _comboFiles;
  private Text _txtKeyTabNew;
  private WGADesignStructureHelper _wgaDesign;
  private LabelTagReplaceHandler _replaceHandeler;

  private Button _buttonCheckPrefix;
  private Combo _comboPrefix;
  private boolean _keyExists;
  private Button _buttonCreateNewLabel;

  public LabelingDialog(Shell parent, ITextSelection textselection, IDocument doc) {
    super(parent);
    setTitle("Labeling");
    _textSelection = textselection;
    _doc = doc;
    _replaceHandeler = new LabelTagReplaceHandler(_doc, "");
    _wgaDesign = new WGADesignStructureHelper(Plugin.getDefault().getActiveFile());
    setHelpAvailable(false);
  }

  @Override
  protected void createButtonsForButtonBar(Composite parent) {
    createButton(parent, IDialogConstants.FINISH_ID, "Create", true);
    _buttonCreateNewLabel = getButton(IDialogConstants.FINISH_ID);
    _buttonCreateNewLabel.setEnabled(false);
  }

  @Override
  protected void buttonPressed(int buttonId) {
    if (IDialogConstants.FINISH_ID == buttonId) {
      handelCreateNewLabel();
    }
  }

  @Override
  protected Control createDialogArea(Composite parent) {

    // prefsString.split(" ")[1].equals("0");

    Composite composite = new Composite(parent, SWT.None);
    composite.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
    GridLayout layout = new GridLayout(2, false);
    composite.setLayout(layout);

    GridData data = new GridData(GridData.FILL, GridData.FILL, true, true);
    data.horizontalSpan = 2;

    GridData txtStyle = new GridData(GridData.FILL_HORIZONTAL);
    txtStyle.minimumWidth = convertWidthInCharsToPixels(50);

    Label fileslbl = new Label(composite, SWT.None);
    fileslbl.setText("Labelfile:");
    _comboFiles = new Combo(composite, SWT.BORDER);
    _comboFiles.setItems(refreshLabelFilesInFolder());
    _comboFiles.setLayoutData(GridDataFactory.copyData(txtStyle));

    String searchFor = "general";
    int i = 0;
    boolean found = false;

    if (Plugin.getDefault().getPreferenceStore().getString((Preferences.LAST_SELECTED_LABELFILE)) != null
        && !Plugin.getDefault().getPreferenceStore().getString(Preferences.LAST_SELECTED_LABELFILE).equals("")) {
      searchFor = Plugin.getDefault().getPreferenceStore().getString((Preferences.LAST_SELECTED_LABELFILE));

      for (String item : _comboFiles.getItems()) {
        if (item.equals(searchFor)) {
          found = true;
          break;
        }

      }

      if (!found) {
        searchFor = "general";
      }
    }

    i = 0;
    for (String item : _comboFiles.getItems()) {
      if (item.equals(searchFor)) {
        break;
      }
      i++;
    }

    _comboFiles.select(i);

    Group group = new Group(composite, SWT.SHADOW_OUT);
    group.setLayoutData(data);
    group.setLayout(layout);

    group.setText("Prefix");

    // new Label(group,SWT.NONE)
    _buttonCheckPrefix = new Button(group, SWT.CHECK);
    _buttonCheckPrefix.setText("use Prefix");
    _buttonCheckPrefix.setLayoutData(data);
   

    Label labelPrefix = new Label(group, SWT.NONE);
    labelPrefix.setText("Prefix :");

    _comboPrefix = new Combo(group, SWT.BORDER | SWT.READ_ONLY);   
    _comboPrefix.setLayoutData(GridDataFactory.copyData(txtStyle));

    _comboPrefix.setEnabled(false);

    // textfield key

    Label keylbl = new Label(composite, SWT.None);
    keylbl.setText("Labelkey:");
    _txtKeyTabNew = new Text(composite, SWT.BORDER);

    _txtKeyTabNew.setLayoutData(GridDataFactory.copyData(txtStyle));   
    _txtKeyTabNew.setFocus();

    _currentLabelContent = WGADesignStructureHelper.loadLabel(getCurrentSelectedLabelContainer().getFile(getCurrentSelectedLabelFileName()));
    refreshPrefix();
    if (Plugin.getDefault().getPreferenceStore().getString((Preferences.LAST_SELECTED_LABELCHECKPREFIX)).equals("0")) {
      _buttonCheckPrefix.setSelection(false);
      _comboPrefix.setEnabled(false);
    } else if (Plugin.getDefault().getPreferenceStore().getString((Preferences.LAST_SELECTED_LABELCHECKPREFIX)).equals("1")) {
      _buttonCheckPrefix.setSelection(true);
      _comboPrefix.setEnabled(true);
      i = 0;
      for (String item : _comboPrefix.getItems()) {
        if (item.equals(Plugin.getDefault().getPreferenceStore().getString((Preferences.LAST_SELECTED_LABELPREFIX))))
          break;
        i++;
      }
      _comboPrefix.select(i);

    }
   
    _txtKeyTabNew.addModifyListener(this);
    _comboPrefix.addSelectionListener(this);
    _buttonCheckPrefix.addSelectionListener(this);
    _comboFiles.addModifyListener(this);
   
    return composite;
  }

  private void refreshPrefix() {
    String[] prefix = getPrefix();
    _comboPrefix.setItems(prefix);
    if (prefix.length == 0) {
      _buttonCheckPrefix.setEnabled(false);
      _buttonCheckPrefix.setSelection(false);
      _comboPrefix.setEnabled(false);
    } else {
      _buttonCheckPrefix.setEnabled(true);
      _buttonCheckPrefix.setSelection(true);
      _comboPrefix.setEnabled(true);
      _comboPrefix.select(0);
     
    }

  }

  private String[] getPrefix() {
    Set<String> prefix = new HashSet<String>();
    Set<Entry<Object, Object>> keyset = _currentLabelContent.entrySet();
    Iterator<Entry<Object, Object>> it = keyset.iterator();
    while (it.hasNext()) {
      Entry<Object, Object> current = it.next();

      String key = current.getKey().toString();
      key = key.substring(0, key.lastIndexOf(".") + 1);
      if (!key.equals("")) {
        prefix.add(key);
      }
    }

    List<String> list = new ArrayList<String>();
    list.addAll(prefix);
    Collections.sort(list);
   
    return list.toArray(new String[0]);
  }


 
  private String[] refreshLabelFilesInFolder() {
    _comboFiles.clearSelection();

    Set<IFile> files = new HashSet<IFile>();
    Set<String> labelFiles = new TreeSet<String>();

    IFolder currentFolder = getCurrentSelectedLabelContainer(); // e.g.
    // labels_de
    if (!currentFolder.exists()) {
      files.add(getCurrentSelectedLabelContainer().getFile("general.properties"));
    } else {
      try {
        files = _wgaDesign.getLabelFiles(currentFolder);
      } catch (CoreException e) {
        Plugin.getDefault().logError(e);
      }
      // adds default file ("general-properties")
      files.add(currentFolder.getFile("general.properties"));
    }

    Iterator<IFile> it = files.iterator();
    while (it.hasNext()) {
      IFile current = it.next();
      String currentFileName = current.getName().substring(0, current.getName().lastIndexOf('.'));
      labelFiles.add(currentFileName);

    }

    List<String> list = new ArrayList<String>();
    list.addAll(labelFiles);
    Collections.sort(list);   
    return list.toArray(new String[0]);

  }

  private String getCurrentSelectedLabelFileName() {
    return _comboFiles.getText().toLowerCase() + ".properties";
  }

  private IFolder getCurrentSelectedLabelContainer() {

    try {
      return _wgaDesign.getFileContainerRoot().getFolder("labels_" + _wgaDesign.getDevelopmentLanguage());
    } catch (CoreException e) {
      Plugin.getDefault().logError(e);
    }
    return null;
  }

  public void validate() {
   
    updateStatus(Status.OK_STATUS);
    if (_buttonCreateNewLabel != null && !_buttonCreateNewLabel.isDisposed()) {
      _buttonCreateNewLabel.setEnabled(true);
    }
    _keyExists = false;

    if (_comboFiles != null && !_comboFiles.isDisposed()) {
      if (!WorkbenchUtils.isValidResourceName(_comboFiles.getText())) {
        updateStatus(new Status(Status.ERROR, Activator.PLUGIN_ID, "specialcharacters in Labelfilename"));
        _buttonCreateNewLabel.setEnabled(false);
      }
      if (_comboFiles.getText().length() == 0) {
        updateStatus(new Status(Status.ERROR, Activator.PLUGIN_ID, "no Labelfile selected"));
        _buttonCreateNewLabel.setEnabled(false);
      }
    }

    if (_txtKeyTabNew != null && !_txtKeyTabNew.isDisposed()) {
      String prefix = "";
      if (_buttonCheckPrefix.getSelection()) {
        prefix = _comboPrefix.getText();
      }

      if (_currentLabelContent.containsKey((prefix + _txtKeyTabNew.getText()))) {
        updateStatus(new Status(Status.WARNING, Activator.PLUGIN_ID, "Key already exists"));
        _keyExists = true;
      }

      if (_txtKeyTabNew.getText().length() == 0) {
        updateStatus(new Status(Status.ERROR, Activator.PLUGIN_ID, "no key set"));
        _buttonCreateNewLabel.setEnabled(false);
      }
      if (_txtKeyTabNew.getText().endsWith(".")) {
        updateStatus(new Status(Status.ERROR, Activator.PLUGIN_ID, "Key ends with \".\""));
        _buttonCreateNewLabel.setEnabled(false);
      }
      if (_txtKeyTabNew.getText().contains("..")) {
        updateStatus(new Status(Status.ERROR, Activator.PLUGIN_ID, "Key contains \"..\""));
        _buttonCreateNewLabel.setEnabled(false);
      }
      if (_txtKeyTabNew.getText().contains(" ")) {
        updateStatus(new Status(Status.ERROR, Activator.PLUGIN_ID, "spaces in keyname"));
        _buttonCreateNewLabel.setEnabled(false);
      }

    }

  }

  public void modifyText(ModifyEvent event) {
   
    _currentLabelContent = WGADesignStructureHelper.loadLabel(getCurrentSelectedLabelContainer().getFile(getCurrentSelectedLabelFileName()));
    _replaceHandeler.setLabelFileName(_comboFiles.getText().toLowerCase());

    if (event.widget.equals(_comboFiles) && _comboPrefix != null) {
      refreshPrefix();
    }

    validate();
  }

  public void widgetDefaultSelected(SelectionEvent e) {

  }

  public void widgetSelected(SelectionEvent event) {
    _currentLabelContent = WGADesignStructureHelper.loadLabel(getCurrentSelectedLabelContainer().getFile(getCurrentSelectedLabelFileName()));

    if (event.widget.equals(_buttonCheckPrefix)) {
      Button check = (Button) event.widget;
      if (check.getSelection()) {
        _comboPrefix.setEnabled(true);

      } else {
        _comboPrefix.setEnabled(false);

      }
    }

    validate();
  }

  private void handelCreateNewLabel() {

    String prefix = "";
    if (_buttonCheckPrefix.getSelection()) {
      prefix = _comboPrefix.getText();
    }

    IFile selectedLabelFile = getCurrentSelectedLabelContainer().getFile(getCurrentSelectedLabelFileName());
    if (!_keyExists) {
      _wgaDesign.createLabel(selectedLabelFile, prefix + _txtKeyTabNew.getText(), _textSelection.getText());
    }
    try {
      _replaceHandeler.replace(prefix + _txtKeyTabNew.getText());

    } catch (BadLocationException e) {
      Plugin.getDefault().logError(e);
    }

    Plugin.getDefault().getPreferenceStore().putValue(Preferences.LAST_SELECTED_LABELFILE, _comboFiles.getText());

    Plugin.getDefault().getPreferenceStore().putValue(Preferences.LAST_SELECTED_LABELCHECKPREFIX, (_buttonCheckPrefix.getSelection() || _txtKeyTabNew.getText().contains(".")) ? "1" : "0");

    Plugin.getDefault().getPreferenceStore().putValue(Preferences.LAST_SELECTED_LABELPREFIX, (prefix + _txtKeyTabNew.getText()).substring(0, (prefix + _txtKeyTabNew.getText()).lastIndexOf(".") + 1));

    close();

  }

  public void computeResponse() {
    // TODO Auto-generated method stub

  }

}
TOP

Related Classes of de.innovationgate.eclipse.dialogs.LabelingDialog

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.