Exemplo n.º 1
0
  /**
   * Read the available profiles from the internal XML file and return them as collection.
   *
   * @return returns a list of <code>CustomProfile</code> or <code>null</code>
   */
  private List<Profile> readOldForCompatibility(IScopeContext instanceScope) {

    // in 3.0 M9 and less the profiles were stored in a file in the plugin's meta data
    final String STORE_FILE = "code_formatter_profiles.xml"; // $NON-NLS-1$

    File file = JavaPlugin.getDefault().getStateLocation().append(STORE_FILE).toFile();
    if (!file.exists()) return null;

    try {
      // note that it's wrong to use a file reader when XML declares UTF-8: Kept for compatibility
      final FileReader reader = new FileReader(file);
      try {
        List<Profile> res = readProfilesFromStream(new InputSource(reader));
        if (res != null) {
          for (int i = 0; i < res.size(); i++) {
            fProfileVersioner.update((CustomProfile) res.get(i));
          }
          writeProfiles(res, instanceScope);
        }
        file.delete(); // remove after successful write
        return res;
      } finally {
        reader.close();
      }
    } catch (CoreException e) {
      JavaPlugin.log(e); // log but ignore
    } catch (IOException e) {
      JavaPlugin.log(e); // log but ignore
    }
    return null;
  }
  public NewJavaProjectPreferencePage() {
    super();
    setPreferenceStore(JavaPlugin.getDefault().getPreferenceStore());
    setDescription(PreferencesMessages.NewJavaProjectPreferencePage_description);

    // title used when opened programatically
    setTitle(PreferencesMessages.NewJavaProjectPreferencePage_title);

    fRadioButtons = new ArrayList<Button>();
    fCheckBoxes = new ArrayList<Button>();
    fTextControls = new ArrayList<Text>();

    fSelectionListener =
        new SelectionListener() {
          public void widgetDefaultSelected(SelectionEvent e) {}

          public void widgetSelected(SelectionEvent e) {
            controlChanged(e.widget);
          }
        };

    fModifyListener =
        new ModifyListener() {
          public void modifyText(ModifyEvent e) {
            controlModified(e.widget);
          }
        };
  }
  private void restoreFromPreferences() {
    String compiledTextHoverModifiers =
        fStore.getString(PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIERS);

    StringTokenizer tokenizer =
        new StringTokenizer(
            compiledTextHoverModifiers, JavaEditorTextHoverDescriptor.VALUE_SEPARATOR);
    HashMap<String, String> idToModifier = new HashMap<String, String>(tokenizer.countTokens() / 2);

    while (tokenizer.hasMoreTokens()) {
      String id = tokenizer.nextToken();
      if (tokenizer.hasMoreTokens()) idToModifier.put(id, tokenizer.nextToken());
    }

    String compiledTextHoverModifierMasks =
        JavaPlugin.getDefault()
            .getPreferenceStore()
            .getString(PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIER_MASKS);

    tokenizer =
        new StringTokenizer(
            compiledTextHoverModifierMasks, JavaEditorTextHoverDescriptor.VALUE_SEPARATOR);
    HashMap<String, String> idToModifierMask =
        new HashMap<String, String>(tokenizer.countTokens() / 2);

    while (tokenizer.hasMoreTokens()) {
      String id = tokenizer.nextToken();
      if (tokenizer.hasMoreTokens()) idToModifierMask.put(id, tokenizer.nextToken());
    }

    for (int i = 0; i < fHoverConfigs.length; i++) {
      String modifierString = idToModifier.get(getContributedHovers()[i].getId());
      boolean enabled = true;
      if (modifierString == null) modifierString = JavaEditorTextHoverDescriptor.DISABLED_TAG;

      if (modifierString.startsWith(JavaEditorTextHoverDescriptor.DISABLED_TAG)) {
        enabled = false;
        modifierString = modifierString.substring(1);
      }

      if (modifierString.equals(JavaEditorTextHoverDescriptor.NO_MODIFIER))
        modifierString = ""; // $NON-NLS-1$

      fHoverConfigs[i].fModifierString = modifierString;
      fHoverConfigs[i].fIsEnabled = enabled;
      fHoverConfigs[i].fStateMask = JavaEditorTextHoverDescriptor.computeStateMask(modifierString);

      if (fHoverConfigs[i].fStateMask == -1) {
        try {
          fHoverConfigs[i].fStateMask =
              Integer.parseInt(idToModifierMask.get(getContributedHovers()[i].getId()));
        } catch (NumberFormatException ex) {
          fHoverConfigs[i].fStateMask = -1;
        }
      }
    }
  }
  private String getIndentMode() {
    String indentMode =
        JavaPlugin.getDefault()
            .getCombinedPreferenceStore()
            .getString(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR);

    if (JavaCore.SPACE.equals(indentMode))
      return PreferencesMessages.SmartTypingConfigurationBlock_tabs_message_spaces;

    if (JavaCore.TAB.equals(indentMode))
      return PreferencesMessages.SmartTypingConfigurationBlock_tabs_message_tabs;

    if (DefaultCodeFormatterConstants.MIXED.equals(indentMode))
      return PreferencesMessages.SmartTypingConfigurationBlock_tabs_message_tabsAndSpaces;

    Assert.isTrue(false, "Illegal indent mode - must not happen"); // $NON-NLS-1$
    return null;
  }
  public static IClasspathEntry[] getDefaultJRELibrary() {
    IPreferenceStore store = JavaPlugin.getDefault().getPreferenceStore();

    String str = store.getString(CLASSPATH_JRELIBRARY_LIST);
    int index = store.getInt(CLASSPATH_JRELIBRARY_INDEX);

    StringTokenizer tok = new StringTokenizer(str, ";"); // $NON-NLS-1$
    while (tok.hasMoreTokens() && index > 0) {
      tok.nextToken();
      index--;
    }

    if (tok.hasMoreTokens()) {
      IClasspathEntry[] res = decodeJRELibraryClasspathEntries(tok.nextToken());
      if (res.length > 0) {
        return res;
      }
    }
    return new IClasspathEntry[] {getJREContainerEntry()};
  }
  public void performOk() {
    StringBuffer buf = new StringBuffer();
    StringBuffer maskBuf = new StringBuffer();
    for (int i = 0; i < fHoverConfigs.length; i++) {
      buf.append(getContributedHovers()[i].getId());
      buf.append(JavaEditorTextHoverDescriptor.VALUE_SEPARATOR);
      if (!fHoverConfigs[i].fIsEnabled) buf.append(JavaEditorTextHoverDescriptor.DISABLED_TAG);
      String modifier = fHoverConfigs[i].fModifierString;
      if (modifier == null || modifier.length() == 0)
        modifier = JavaEditorTextHoverDescriptor.NO_MODIFIER;
      buf.append(modifier);
      buf.append(JavaEditorTextHoverDescriptor.VALUE_SEPARATOR);

      maskBuf.append(getContributedHovers()[i].getId());
      maskBuf.append(JavaEditorTextHoverDescriptor.VALUE_SEPARATOR);
      maskBuf.append(fHoverConfigs[i].fStateMask);
      maskBuf.append(JavaEditorTextHoverDescriptor.VALUE_SEPARATOR);
    }
    fStore.setValue(PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIERS, buf.toString());
    fStore.setValue(PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIER_MASKS, maskBuf.toString());

    JavaPlugin.getDefault().resetJavaEditorTextHoverDescriptors();
  }
 /*
  * @see org.org.eclipse.ui.internal.editors.text.AbstractConfigurationBlockPreferencePage#setPreferenceStore()
  */
 @Override
 protected void setPreferenceStore() {
   setPreferenceStore(JavaPlugin.getDefault().getPreferenceStore());
 }
Exemplo n.º 8
0
 private static IPreferenceStore getPreferenceStore() {
   return JavaPlugin.getDefault().getCombinedPreferenceStore();
 }
 private JavaEditorTextHoverDescriptor[] getContributedHovers() {
   return JavaPlugin.getDefault().getJavaEditorTextHoverDescriptors();
 }
  private void createMessage(final Composite composite) {
    // TODO create a link with an argument, so the formatter preference page can open the
    // current profile automatically.
    String linkTooltip = PreferencesMessages.SmartTypingConfigurationBlock_tabs_message_tooltip;
    String text;
    String indentMode =
        JavaPlugin.getDefault()
            .getCombinedPreferenceStore()
            .getString(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR);
    if (JavaCore.TAB.equals(indentMode))
      text =
          Messages.format(
              PreferencesMessages.SmartTypingConfigurationBlock_tabs_message_tab_text,
              new String[] {Integer.toString(getTabDisplaySize())});
    else
      text =
          Messages.format(
              PreferencesMessages.SmartTypingConfigurationBlock_tabs_message_others_text,
              new String[] {
                Integer.toString(getTabDisplaySize()),
                Integer.toString(getIndentSize()),
                getIndentMode()
              });

    final Link link = new Link(composite, SWT.NONE);
    link.setText(text);
    link.setToolTipText(linkTooltip);
    GridData gd = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
    gd.widthHint = 300; // don't get wider initially
    link.setLayoutData(gd);
    link.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            PreferencesUtil.createPreferenceDialogOn(
                link.getShell(),
                "org.eclipse.jdt.ui.preferences.CodeFormatterPreferencePage",
                null,
                null); //$NON-NLS-1$
          }
        });

    final IPreferenceStore combinedStore = JavaPlugin.getDefault().getCombinedPreferenceStore();
    final IPropertyChangeListener propertyChangeListener =
        new IPropertyChangeListener() {
          private boolean fHasRun = false;

          public void propertyChange(PropertyChangeEvent event) {
            if (fHasRun) return;
            if (composite.isDisposed()) return;
            String property = event.getProperty();
            if (DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR.equals(property)
                || DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE.equals(property)) {
              fHasRun = true;
              link.dispose();
              createMessage(composite);
              Dialog.applyDialogFont(composite);
              composite.redraw();
              composite.layout();
            }
          }
        };
    combinedStore.addPropertyChangeListener(propertyChangeListener);
    link.addDisposeListener(
        new DisposeListener() {
          public void widgetDisposed(org.eclipse.swt.events.DisposeEvent e) {
            combinedStore.removePropertyChangeListener(propertyChangeListener);
          }
        });
  }