Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #77 - Project level settings to choose default new files exten…
…sions.
- Loading branch information
Showing
14 changed files
with
464 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/org/intellij/xquery/actions/XQueryCreateFromTemplateHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
23
src/main/java/org/intellij/xquery/actions/XQueryFileTemplates.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/main/java/org/intellij/xquery/settings/DefaultFileExtensionsPanel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
25
src/main/java/org/intellij/xquery/settings/SettingsPanel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.