computing template module type. corresponding steps update

This commit is contained in:
Dmitry Avdeev
2012-10-19 13:04:32 +04:00
parent 84f621e7e3
commit bbb8f0329f
3 changed files with 173 additions and 89 deletions
@@ -181,33 +181,10 @@ public class SelectTemplateStep extends ModuleWizardStep {
myTemplatesTree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
if (mySettingsPanel.getComponentCount() > 0) {
mySettingsPanel.remove(0);
}
ProjectTemplate template = getSelectedTemplate();
if (template != null) {
JComponent settingsPanel = template.getSettingsPanel();
if (settingsPanel != null) {
mySettingsPanel.add(settingsPanel, BorderLayout.NORTH);
}
mySettingsPanel.setVisible(settingsPanel != null);
String description = template.getDescription();
if (StringUtil.isNotEmpty(description)) {
StringBuilder sb = new StringBuilder("<html><body><font face=\"Verdana\" ");
sb.append(SystemInfo.isMac ? "" : "size=\"-1\"").append('>');
sb.append(description).append("</font></body></html>");
description = sb.toString();
}
myDescriptionPane.setText(description);
myDescriptionPanel.setVisible(StringUtil.isNotEmpty(description));
}
else {
mySettingsPanel.setVisible(false);
myDescriptionPanel.setVisible(false);
}
mySettingsPanel.revalidate();
mySettingsPanel.repaint();
setupPanels(template);
mySequence.setType(template == null ? null : template.createModuleBuilder().getBuilderId());
myContext.requestWizardButtonsUpdate();
}
});
@@ -223,13 +200,6 @@ public class SelectTemplateStep extends ModuleWizardStep {
myDescriptionPanel.setVisible(false);
mySettingsPanel.setVisible(false);
TreeState state = SelectTemplateSettings.getInstance().getTreeState();
if (state != null) {
state.applyTo(myTemplatesTree, (DefaultMutableTreeNode)myTemplatesTree.getModel().getRoot());
}
else {
myBuilder.expandAll(null);
}
new AnAction() {
@Override
@@ -248,6 +218,49 @@ public class SelectTemplateStep extends ModuleWizardStep {
}
}
}.registerCustomShortcutSet(new CustomShortcutSet(KeyEvent.VK_UP, KeyEvent.VK_DOWN), mySearchField);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TreeState state = SelectTemplateSettings.getInstance().getTreeState();
if (state != null) {
state.applyTo(myTemplatesTree, (DefaultMutableTreeNode)myTemplatesTree.getModel().getRoot());
}
else {
myBuilder.expandAll(null);
}
}
});
}
private void setupPanels(@Nullable ProjectTemplate template) {
if (mySettingsPanel.getComponentCount() > 0) {
mySettingsPanel.remove(0);
}
if (template != null) {
JComponent settingsPanel = template.getSettingsPanel();
if (settingsPanel != null) {
mySettingsPanel.add(settingsPanel, BorderLayout.NORTH);
}
mySettingsPanel.setVisible(settingsPanel != null);
String description = template.getDescription();
if (StringUtil.isNotEmpty(description)) {
StringBuilder sb = new StringBuilder("<html><body><font face=\"Verdana\" ");
sb.append(SystemInfo.isMac ? "" : "size=\"-1\"").append('>');
sb.append(description).append("</font></body></html>");
description = sb.toString();
}
myDescriptionPane.setText(description);
myDescriptionPanel.setVisible(StringUtil.isNotEmpty(description));
}
else {
mySettingsPanel.setVisible(false);
myDescriptionPanel.setVisible(false);
}
mySettingsPanel.revalidate();
mySettingsPanel.repaint();
}
@Override
@@ -15,32 +15,16 @@
*/
package com.intellij.platform.templates;
import com.intellij.ide.util.newProjectWizard.modes.ImportImlMode;
import com.intellij.ide.util.projectWizard.ModuleBuilder;
import com.intellij.ide.util.projectWizard.WizardContext;
import com.intellij.openapi.module.ModifiableModuleModel;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.module.ModuleWithNameAlreadyExists;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.io.StreamUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.newvfs.RefreshQueue;
import com.intellij.platform.ProjectTemplate;
import com.intellij.platform.templates.github.ZipUtil;
import com.intellij.util.containers.ContainerUtil;
import org.jdom.JDOMException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.zip.ZipEntry;
@@ -74,11 +58,22 @@ public class ArchivedProjectTemplate implements ProjectTemplate {
@Override
public String getDescription() {
return readEntry(new Condition<ZipEntry>() {
@Override
public boolean value(ZipEntry entry) {
return entry.getName().endsWith(DESCRIPTION_PATH);
}
});
}
@Nullable
String readEntry(Condition<ZipEntry> condition) {
ZipInputStream stream = null;
try {
ZipInputStream stream = getStream();
stream = getStream();
ZipEntry entry;
while ((entry = stream.getNextEntry()) != null) {
if (entry.getName().endsWith(DESCRIPTION_PATH)) {
if (condition.value(entry)) {
return StreamUtil.readText(stream);
}
}
@@ -86,58 +81,26 @@ public class ArchivedProjectTemplate implements ProjectTemplate {
catch (IOException e) {
return null;
}
finally {
StreamUtil.closeStream(stream);
}
return null;
}
@NotNull
@Override
public ModuleBuilder createModuleBuilder() {
return new ModuleBuilder() {
@Override
public void setupRootModel(ModifiableRootModel modifiableRootModel) throws ConfigurationException {
}
@Override
public ModuleType getModuleType() {
return null;
}
@NotNull
@Override
public Module createModule(@NotNull ModifiableModuleModel moduleModel)
throws InvalidDataException, IOException, ModuleWithNameAlreadyExists, JDOMException, ConfigurationException {
final String path = getContentEntryPath();
String iml;
try {
File dir = new File(path);
ZipInputStream zipInputStream = getStream();
ZipUtil.unzip(ProgressManager.getInstance().getProgressIndicator(), dir, zipInputStream);
VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(dir);
iml = ContainerUtil.find(dir.list(), new Condition<String>() {
@Override
public boolean value(String s) {
return s.endsWith(".iml");
}
});
new File(path, iml).renameTo(new File(getModuleFilePath()));
RefreshQueue.getInstance().refresh(false, true, null, virtualFile);
}
catch (IOException e) {
throw new RuntimeException(e);
}
return ImportImlMode.setUpLoader(getModuleFilePath()).createModule(moduleModel);
}
};
return new TemplateModuleBuilder(this);
}
@Nullable
@Override
public ValidationInfo validateSettings() {
return null;
}
private ZipInputStream getStream() throws IOException {
ZipInputStream getStream() throws IOException {
return new ZipInputStream(myArchivePath.openStream());
}
@@ -0,0 +1,108 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* 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 com.intellij.platform.templates;
import com.intellij.ide.util.newProjectWizard.modes.ImportImlMode;
import com.intellij.ide.util.projectWizard.ModuleBuilder;
import com.intellij.openapi.module.*;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.newvfs.RefreshQueue;
import com.intellij.platform.templates.github.ZipUtil;
import com.intellij.util.containers.ContainerUtil;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* @author Dmitry Avdeev
* Date: 10/19/12
*/
class TemplateModuleBuilder extends ModuleBuilder {
private final ModuleType myType;
private ArchivedProjectTemplate myTemplate;
public TemplateModuleBuilder(ArchivedProjectTemplate template) {
myTemplate = template;
myType = computeModuleType(myTemplate);
}
@NotNull
private static ModuleType computeModuleType(ArchivedProjectTemplate template) {
String iml = template.readEntry(new Condition<ZipEntry>() {
@Override
public boolean value(ZipEntry entry) {
return entry.getName().endsWith(".iml");
}
});
if (iml == null) return ModuleType.EMPTY;
try {
Document document = JDOMUtil.loadDocument(iml);
String type = document.getRootElement().getAttributeValue(Module.ELEMENT_TYPE);
return ModuleTypeManager.getInstance().findByID(type);
}
catch (Exception e) {
return ModuleType.EMPTY;
}
}
@Override
public void setupRootModel(ModifiableRootModel modifiableRootModel) throws ConfigurationException {
}
@Override
public ModuleType getModuleType() {
return myType;
}
@NotNull
@Override
public Module createModule(@NotNull ModifiableModuleModel moduleModel)
throws InvalidDataException, IOException, ModuleWithNameAlreadyExists, JDOMException, ConfigurationException {
final String path = getContentEntryPath();
String iml;
try {
File dir = new File(path);
ZipInputStream zipInputStream = myTemplate.getStream();
ZipUtil.unzip(ProgressManager.getInstance().getProgressIndicator(), dir, zipInputStream);
VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(dir);
iml = ContainerUtil.find(dir.list(), new Condition<String>() {
@Override
public boolean value(String s) {
return s.endsWith(".iml");
}
});
new File(path, iml).renameTo(new File(getModuleFilePath()));
RefreshQueue.getInstance().refresh(false, true, null, virtualFile);
}
catch (IOException e) {
throw new RuntimeException(e);
}
return ImportImlMode.setUpLoader(getModuleFilePath()).createModule(moduleModel);
}
}