Skip to content

Commit

Permalink
Closes #77 - Project level settings to choose default new files exten…
Browse files Browse the repository at this point in the history
…sions.
  • Loading branch information
ligasgr committed May 25, 2014
1 parent e3ac550 commit 0b5b81b
Show file tree
Hide file tree
Showing 14 changed files with 464 additions and 19 deletions.
5 changes: 5 additions & 0 deletions src/main/java/org/intellij/xquery/XQueryFileType.java
Expand Up @@ -23,6 +23,9 @@
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.List;

import static java.util.Arrays.asList;

/**
* User: ligasgr
Expand All @@ -34,6 +37,8 @@ public class XQueryFileType extends LanguageFileType {
public static final String DEFAULT_EXTENSION = "xq";
public static final String DEFAULT_EXTENSION_WITH_DOT = "." + DEFAULT_EXTENSION;
public static final String ALL_EXTENSIONS = "xq;xqi;xql;xqm;xqy;xqws;xquery";
static final String EXTENSION_SEPARATOR = ";";
public static final List<String> ALL_EXTENSIONS_LIST = asList(ALL_EXTENSIONS.split(EXTENSION_SEPARATOR));

private XQueryFileType() {
super(XQueryLanguage.INSTANCE);
Expand Down
Expand Up @@ -25,8 +25,13 @@
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiDirectory;
import org.intellij.xquery.XQueryFileType;
import org.intellij.xquery.icons.XQueryIcons;

import static org.intellij.xquery.XQueryFileType.DEFAULT_EXTENSION_WITH_DOT;
import static org.intellij.xquery.actions.XQueryFileTemplates.LIBRARY_MODULE;
import static org.intellij.xquery.actions.XQueryFileTemplates.MAIN_MODULE;

/**
* User: ligasgr
* Date: 08/09/13
Expand Down Expand Up @@ -55,8 +60,8 @@ protected void buildDialog(final Project project, PsiDirectory psiDirectory, Cre
builder) {
builder
.setTitle(NEW_XQUERY_FILE)
.addKind("Library Module", XQueryIcons.FILE, "XQuery Library Module.xq")
.addKind("Main Module", XQueryIcons.FILE, "XQuery Main Module.xq")
.addKind("Library Module", XQueryIcons.FILE, LIBRARY_MODULE + DEFAULT_EXTENSION_WITH_DOT)
.addKind("Main Module", XQueryIcons.FILE, MAIN_MODULE + DEFAULT_EXTENSION_WITH_DOT)
.setValidator(getValidator());
}

Expand All @@ -81,7 +86,7 @@ public boolean canClose(String inputString) {
public String getErrorText(String inputString) {
String error = " is not a valid XQuery Module name";
if (StringUtil.isEmpty(inputString)) return null;
if (inputString != null && inputString.equals(FileUtil.sanitizeFileName(inputString))) {
if (inputString.equals(FileUtil.sanitizeFileName(inputString))) {
return null;
}
return "'" + inputString + "'" + error;
Expand Down
@@ -0,0 +1,72 @@
/*
* Copyright 2013-2014 Grzegorz Ligas <ligasgr@gmail.com> and other contributors
* (see the CONTRIBUTORS file).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.intellij.xquery.actions;

import com.intellij.ide.fileTemplates.DefaultCreateFromTemplateHandler;
import com.intellij.ide.fileTemplates.FileTemplate;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.ex.FileTypeManagerEx;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import org.intellij.xquery.XQueryFileType;
import org.intellij.xquery.settings.XQuerySettings;

import java.util.Map;

import static org.intellij.xquery.actions.XQueryFileTemplates.LIBRARY_MODULE;
import static org.intellij.xquery.actions.XQueryFileTemplates.MAIN_MODULE;

public class XQueryCreateFromTemplateHandler extends DefaultCreateFromTemplateHandler {
private Project project;

@Override
public PsiElement createFromTemplate(Project project, PsiDirectory directory, String fileName, FileTemplate template, String templateText, Map<String, Object> props) throws IncorrectOperationException {
this.project = project;
return super.createFromTemplate(project, directory, fileName, template, templateText, props);
}

@Override
public boolean handlesTemplate(FileTemplate template) {
FileType fileType = FileTypeManagerEx.getInstanceEx().getFileTypeByExtension(template.getExtension());
return fileType.equals(XQueryFileType.INSTANCE);
}

@Override
protected String checkAppendExtension(String fileName, FileTemplate template) {
XQuerySettings settings = XQuerySettings.getInstance(project);
String extension = getExtension(template, settings);
final String suggestedFileNameEnd = "." + extension;

if (!fileName.endsWith(suggestedFileNameEnd)) {
fileName += suggestedFileNameEnd;
}
return fileName;
}

private String getExtension(FileTemplate template, XQuerySettings settings) {
String extension = template.getExtension();
if (MAIN_MODULE.equals(template.getName()) && settings.getDefaultMainModuleExtension() != null) {
extension = settings.getDefaultMainModuleExtension();
} else if (LIBRARY_MODULE.equals(template.getName()) && settings.getDefaultLibraryModuleExtension() != null) {
extension = settings.getDefaultLibraryModuleExtension();
}
return extension;
}
}
23 changes: 23 additions & 0 deletions src/main/java/org/intellij/xquery/actions/XQueryFileTemplates.java
@@ -0,0 +1,23 @@
/*
* Copyright 2013-2014 Grzegorz Ligas <ligasgr@gmail.com> and other contributors
* (see the CONTRIBUTORS file).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.intellij.xquery.actions;

public class XQueryFileTemplates {
public static final String LIBRARY_MODULE = "XQuery Library Module";
public static final String MAIN_MODULE = "XQuery Main Module";
}
Expand Up @@ -19,7 +19,8 @@

import com.intellij.openapi.fileChooser.FileTypeDescriptor;
import com.intellij.openapi.vfs.VirtualFile;
import org.intellij.xquery.XQueryFileType;

import static org.intellij.xquery.XQueryFileType.ALL_EXTENSIONS_LIST;

/**
* User: ligasgr
Expand All @@ -28,11 +29,10 @@
*/
public class ModuleFileDescriptor extends FileTypeDescriptor {

private static final String EXTENSION_SEPARATOR = ";";
private ModuleTypeValidator moduleTypeValidator;

public ModuleFileDescriptor(ModuleTypeValidator moduleTypeValidator) {
super("XQuery module", XQueryFileType.ALL_EXTENSIONS.split(EXTENSION_SEPARATOR));
super("XQuery module", ALL_EXTENSIONS_LIST.toArray(new String[ALL_EXTENSIONS_LIST.size()]));
this.moduleTypeValidator = moduleTypeValidator;
}

Expand Down
@@ -0,0 +1,93 @@
/*
* Copyright 2013-2014 Grzegorz Ligas <ligasgr@gmail.com> and other contributors
* (see the CONTRIBUTORS file).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.intellij.xquery.settings;

import com.intellij.openapi.ui.LabeledComponent;
import com.intellij.ui.SortedComboBoxModel;
import net.miginfocom.swing.MigLayout;

import javax.swing.*;
import java.util.Comparator;
import java.util.List;

public class DefaultFileExtensionsPanel extends SettingsPanel {

public static final String MAIN_MODULE_FILE_EXTENSION = "mainModuleFileExtension";
public static final String LIBRARY_MODULE_FILE_EXTENSION = "libraryModuleFileExtension";
private final LabeledComponent<JComboBox> mainModuleFileExtension;
private final LabeledComponent<JComboBox> libraryModuleFileExtension;
private final SortedComboBoxModel<Object> mainModuleFileExtensionModel = comboBoxModel();
private final SortedComboBoxModel<Object> libraryModuleFileExtensionModel = comboBoxModel();
private final String defaultMainModuleExtension;
private final String defaultLibraryModuleExtension;

public DefaultFileExtensionsPanel(String defaultMainModuleExtension, String defaultLibraryModuleExtension, List<String> allExtensions) {
this.defaultMainModuleExtension = defaultMainModuleExtension;
this.defaultLibraryModuleExtension = defaultLibraryModuleExtension;
setLayout(new MigLayout("ins 0, gap 5, fill, flowy"));
mainModuleFileExtension = comboBox("&Main module", MAIN_MODULE_FILE_EXTENSION, mainModuleFileExtensionModel);
libraryModuleFileExtension = comboBox("&Library module", LIBRARY_MODULE_FILE_EXTENSION, libraryModuleFileExtensionModel);
add(mainModuleFileExtension);
add(libraryModuleFileExtension);

setBorder(BorderFactory.createTitledBorder("Default new file extensions"));
populateExtensionsList(mainModuleFileExtensionModel, defaultMainModuleExtension, allExtensions);
populateExtensionsList(libraryModuleFileExtensionModel, defaultLibraryModuleExtension, allExtensions);
}

@Override
public XQuerySettings updateSettings(XQuerySettings settings) {
settings.setDefaultMainModuleExtension((String) mainModuleFileExtension.getComponent().getSelectedItem());
settings.setDefaultLibraryModuleExtension((String) libraryModuleFileExtension.getComponent().getSelectedItem());
return settings;
}

@Override
public void updatePanel(XQuerySettings settings) {
String mainFileExtension = settings.getDefaultMainModuleExtension() != null ? settings.getDefaultMainModuleExtension() : defaultMainModuleExtension;
String libraryFileExtension = settings.getDefaultLibraryModuleExtension() != null ? settings.getDefaultLibraryModuleExtension() : defaultLibraryModuleExtension;
mainModuleFileExtension.getComponent().setSelectedItem(mainFileExtension);
libraryModuleFileExtension.getComponent().setSelectedItem(libraryFileExtension);
}

private void populateExtensionsList(SortedComboBoxModel<Object> model, Object defaultItem, List<String> allItems) {
for (String type : allItems) {
model.add(type);
}
model.setSelectedItem(defaultItem);
}

private SortedComboBoxModel<Object> comboBoxModel() {
return new SortedComboBoxModel<Object>(new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
return ((String) o1).compareToIgnoreCase((String) o2);
}
});
}

private LabeledComponent<JComboBox> comboBox(String text, String name, SortedComboBoxModel<Object> model) {
LabeledComponent<JComboBox> comboBox = new LabeledComponent<JComboBox>();
comboBox.setText(text);
comboBox.setLabelLocation("West");
comboBox.setComponent(new JComboBox());
comboBox.getComponent().setName(name);
comboBox.getComponent().setModel(model);
return comboBox;
}
}
25 changes: 25 additions & 0 deletions src/main/java/org/intellij/xquery/settings/SettingsPanel.java
@@ -0,0 +1,25 @@
/*
* Copyright 2013-2014 Grzegorz Ligas <ligasgr@gmail.com> and other contributors
* (see the CONTRIBUTORS file).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.intellij.xquery.settings;

import javax.swing.*;

public abstract class SettingsPanel extends JPanel {
public abstract XQuerySettings updateSettings(XQuerySettings settings);
public abstract void updatePanel(XQuerySettings settings);
}
44 changes: 40 additions & 4 deletions src/main/java/org/intellij/xquery/settings/XQuerySettings.java
Expand Up @@ -37,7 +37,8 @@
})
public class XQuerySettings implements PersistentStateComponent<XQuerySettings> {

private String flavour = "XQuery 3.0 Standard";
private String defaultMainModuleExtension;
private String defaultLibraryModuleExtension;

public static XQuerySettings getInstance(Project project) {
return ServiceManager.getService(project, XQuerySettings.class);
Expand All @@ -54,8 +55,43 @@ public void loadState(XQuerySettings state) {
XmlSerializerUtil.copyBean(state, this);
}

@Tag("flavour")
public String getFlavour() {
return flavour;
@Tag("defaultMainModuleExtension")
public String getDefaultMainModuleExtension() {
return defaultMainModuleExtension;
}

public void setDefaultMainModuleExtension(String defaultMainModuleExtension) {
this.defaultMainModuleExtension = defaultMainModuleExtension;
}

@Tag("defaultLibraryModuleExtension")
public String getDefaultLibraryModuleExtension() {
return defaultLibraryModuleExtension;
}

public void setDefaultLibraryModuleExtension(String defaultLibraryModuleExtension) {
this.defaultLibraryModuleExtension = defaultLibraryModuleExtension;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

XQuerySettings that = (XQuerySettings) o;

if (defaultLibraryModuleExtension != null ? !defaultLibraryModuleExtension.equals(that.defaultLibraryModuleExtension) : that.defaultLibraryModuleExtension != null)
return false;
if (defaultMainModuleExtension != null ? !defaultMainModuleExtension.equals(that.defaultMainModuleExtension) : that.defaultMainModuleExtension != null)
return false;

return true;
}

@Override
public int hashCode() {
int result = defaultMainModuleExtension != null ? defaultMainModuleExtension.hashCode() : 0;
result = 31 * result + (defaultLibraryModuleExtension != null ? defaultLibraryModuleExtension.hashCode() : 0);
return result;
}
}
22 changes: 17 additions & 5 deletions src/main/java/org/intellij/xquery/settings/XQuerySettingsForm.java
Expand Up @@ -17,28 +17,40 @@

package org.intellij.xquery.settings;

import net.miginfocom.swing.MigLayout;

import javax.swing.*;

public class XQuerySettingsForm {
public static final String SETTINGS_PANEL_NAME = "settingsPanel";
private XQuerySettings settings;
private final SettingsPanel defaultFileExtensionsPanel;
private JPanel rootPanel;
private XQuerySettings originalSettings;

public JComponent getFormComponent() {
public XQuerySettingsForm(SettingsPanel defaultFileExtensionsPanel) {
rootPanel = new JPanel();
rootPanel.setLayout(new MigLayout("ins 0, gap 5, fill, flowy"));
rootPanel.setName(SETTINGS_PANEL_NAME);
rootPanel.add(defaultFileExtensionsPanel, "growx, ay top");
this.defaultFileExtensionsPanel = defaultFileExtensionsPanel;
}

public JComponent getFormComponent() {
return rootPanel;
}

public boolean isModified() {
return false;
return !originalSettings.equals(getSettings());
}

public XQuerySettings getSettings() {
return settings;
XQuerySettings newSettings = new XQuerySettings();
defaultFileExtensionsPanel.updateSettings(newSettings);
return newSettings;
}

public void setSettings(XQuerySettings settings) {
this.settings = settings;
this.originalSettings = settings;
defaultFileExtensionsPanel.updatePanel(settings);
}
}

0 comments on commit 0b5b81b

Please sign in to comment.