Exemplo n.º 1
0
  protected void addSelection() {
    String key = keyText.getText();
    String val = valueText.getText();
    if (key != null && key.trim().length() > 0) {
      boolean isContained = false;
      for (int i = 0; i < globalLinkedProperties.size(); i++) {
        GlobalProperty property = (GlobalProperty) globalLinkedProperties.get(i);
        if (key.equals(property.key) && !property.isDeleted) {
          property.value = val;
          isContained = true;
          break;
        }
      }

      if (!isContained) {
        // if the file is read-only then change is not allowed.
        if (propFileName[0] == null) {
          return;
        } else {
          File f = new File(propFileName[0]);
          if (!(f.exists() && f.isFile())) {
            MessageDialog.openError(
                getShell(),
                Messages.getString("ResourceEditDialog.NotFile.Title"), // $NON-NLS-1$
                Messages.getFormattedString(
                    "ResourceEditDialog.NotFile.Message", //$NON-NLS-1$
                    new Object[] {propFileName}));
            return;
          } else if (!f.canWrite()) {
            MessageDialog.openError(
                getShell(),
                Messages.getString("ResourceEditDialog.ReadOnlyEncounter.Title"), // $NON-NLS-1$
                Messages.getFormattedString(
                    "ResourceEditDialog.ReadOnlyEncounter.Message", //$NON-NLS-1$
                    new Object[] {propFileName}));
            return;
          }

          GlobalProperty property = new GlobalProperty();
          property.key = key;
          property.value = val;
          property.holder = contents[0];
          property.isDeleted = false;
          property.holderFile = propFileName[0];

          globalLinkedProperties.add(property);
        }
      }

      viewer.refresh();
      listChanged = true;
      updateSelection();

    } else {
      MessageDialog.openWarning(
          getShell(),
          Messages.getString("ResourceEditDialog.text.AddWarningTitle"), // $NON-NLS-1$
          Messages.getString("ResourceEditDialog.text.AddWarningMsg")); // $NON-NLS-1$
    }
  }
Exemplo n.º 2
0
  private boolean saveFile(String filePath, LinkedProperties properties, String fileName) {
    File f = new File(filePath);
    if (!(f.exists() && f.isFile())) {
      MessageDialog.openError(
          getShell(),
          Messages.getString("ResourceEditDialog.NotFile.Title"), // $NON-NLS-1$
          Messages.getFormattedString(
              "ResourceEditDialog.NotFile.Message", //$NON-NLS-1$
              new Object[] {propFileName}));
      return false;
    } else if (!f.canWrite()) {
      MessageDialog.openError(
          getShell(),
          Messages.getString("ResourceEditDialog.ReadOnlyEncounter.Title"), // $NON-NLS-1$
          Messages.getFormattedString(
              "ResourceEditDialog.ReadOnlyEncounter.Message", //$NON-NLS-1$
              new Object[] {propFileName}));
      return false;
    }

    FileOutputStream fos = null;
    try {
      if (f.canWrite()) {
        fos = new FileOutputStream(f);

        properties.store(fos, ""); // $NON-NLS-1$
      }
      return true;
    } catch (Exception e) {
      ExceptionHandler.handle(e);
    } finally {
      if (fos != null) {
        try {
          fos.close();
        } catch (IOException e) {
          ExceptionHandler.handle(e);
        }
      }
    }
    return false;
  }
Exemplo n.º 3
0
  private void deleteSelection() {
    if (viewer.getTable().getSelectionIndex() == -1) {
      return;
    }

    StructuredSelection selection = (StructuredSelection) viewer.getSelection();
    if (selection.getFirstElement() instanceof GlobalProperty) {
      GlobalProperty property = (GlobalProperty) selection.getFirstElement();
      String file = property.holderFile;
      if (file != null) {
        File f = new File(file);
        if (!(f.exists() && f.isFile())) {
          MessageDialog.openError(
              getShell(),
              Messages.getString("ResourceEditDialog.NotFile.Title"), // $NON-NLS-1$
              Messages.getFormattedString(
                  "ResourceEditDialog.NotFile.Message", //$NON-NLS-1$
                  new Object[] {file}));
          return;
        } else if (!f.canWrite()) {
          MessageDialog.openError(
              getShell(),
              Messages.getString("ResourceEditDialog.ReadOnlyEncounter.Title"), // $NON-NLS-1$
              Messages.getFormattedString(
                  "ResourceEditDialog.ReadOnlyEncounter.Message", //$NON-NLS-1$
                  new Object[] {file}));
          return;
        }
      }

      // if the file is read-only then change is not allowed.
      listChanged = true;
      if (property.holderFile != null) property.isDeleted = true;
      else globalLinkedProperties.remove(property);
      viewer.refresh();
      updateSelection();
    }
  }