Skip to content

Commit

Permalink
Merge pull request #7 from tangcent/master
Browse files Browse the repository at this point in the history
Base Code of UITemplateEditor&Builder
  • Loading branch information
tangcent authored Apr 5, 2020
2 parents 22df96d + 4c89273 commit 55048b8
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ public AbstractTemplateEditor(EditableTemplate template) {
this.template = template;
}

@Override
public void show() {
renderTemplate(template);
}

protected abstract void renderTemplate(Template template);

protected abstract String name();

protected abstract String displayName();
Expand All @@ -40,6 +33,5 @@ public void save() {
saveTemplate(template);
}

protected abstract void saveTemplate(Template template);

protected abstract void saveTemplate(EditableTemplate template);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.itangcent.icode.api;


public interface EditableTemplateManager extends TemplateManager {

/**
* Find template by name.
* the Template get from EditableTemplateManager should be editable
*
* @param name -template name
* @return the template which is editable
*/
@Override
EditableTemplate findByName(String name);

/**
* Find template by name and context.
* The Template get from EditableTemplateManager should be editable
*
* @param name -template name
* @return the template which is editable
*/
@Override
EditableTemplate findByName(String name, TemplateContext context);

/**
* Return true if any template was modified.
*/
boolean isAnyModified();

/**
* Save all changes.
*/
void save();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

public interface TemplateEditor {

void show();

void cancel();
void reset();

void save();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public interface TemplateEditorBuilder {

TemplateEditor createEditor(Template template);
TemplateEditor createEditor(EditableTemplate template);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@

public interface TemplateManager {

/**
* Find template by name.
*
* @param name -template name
* @return the template which is editable
*/
default Template findByName(String name) {
return findByName(name, null);
}

/**
* Find template by name and context.
*
* @param name -template name
* @return the template which is editable
*/
Template findByName(String name, TemplateContext context);
}
115 changes: 115 additions & 0 deletions icodeless-core/src/main/java/com/itangcent/icode/ui/UIContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.itangcent.icode.ui;

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

public interface UIContainer {

/**
* Appends the specified component to the end of this container.
* <p>
* This method changes layout-related information, and therefore,
* invalidates the component hierarchy. If the container has already been
* displayed, the hierarchy must be validated thereafter in order to
* display the added component.
*
* @param comp the component to be added
* @return the component argument
* @throws NullPointerException if {@code comp} is {@code null}
* @see javax.swing.JComponent#revalidate()
*/
Component add(Component comp);

/**
* Removes the <code>Viewport</code>s one lightweight child.
*/
void remove(Component child);

/**
* remove all sub components
*/
void clear();


static UIContainer of(Container container) {
if (container instanceof JScrollPane) {
return new JScrollPaneUIContainer((JScrollPane) container);
} else {
return new BaseUIContainer(container);
}
}

abstract class AbstractUIContainer implements UIContainer {

private List<Component> subs = new ArrayList<>();

@Override
public Component add(Component comp) {
subs.add(comp);
return doAdd(comp);
}

@Override
public void remove(Component child) {
subs.remove(child);
doRemove(child);
}

abstract Component doAdd(Component comp);

abstract void doRemove(Component child);

@Override
public void clear() {
if (!subs.isEmpty()) {
for (Component sub : subs) {
doRemove(sub);
}
}
subs.clear();
}
}

class BaseUIContainer extends AbstractUIContainer {

private Container container;

private BaseUIContainer(Container container) {
this.container = container;
}

@Override
public Component doAdd(Component comp) {
return container.add(comp);
}

@Override
public void doRemove(Component child) {
container.remove(child);
}

}

class JScrollPaneUIContainer extends AbstractUIContainer {


private JScrollPane container;

private JScrollPaneUIContainer(JScrollPane container) {
this.container = container;
}

@Override
public Component doAdd(Component comp) {
return container.getViewport().add(comp);
}

@Override
public void doRemove(Component child) {
container.getViewport().remove(child);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.itangcent.icode.ui;

import com.itangcent.icode.api.AbstractTemplateEditor;
import com.itangcent.icode.api.EditableTemplate;

import java.awt.*;

public abstract class UITemplateEditor extends AbstractTemplateEditor {

public UITemplateEditor(EditableTemplate template) {
super(template);
}

public abstract Component createUI();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.itangcent.icode.ui;

import com.itangcent.icode.api.EditableTemplate;
import com.itangcent.icode.api.TemplateEditor;
import com.itangcent.icode.api.TemplateEditorBuilder;

import java.awt.*;

public abstract class UITemplateEditorBuilder implements TemplateEditorBuilder {

/**
* The parent container for rendering the template editor
*/
private UIContainer parentContainer;

public UITemplateEditorBuilder(Container parentContainer) {
this.parentContainer = UIContainer.of(parentContainer);
}

@Override
public TemplateEditor createEditor(EditableTemplate template) {
UITemplateEditor uiTemplateEditor = buildEditor(template);
addEditorUIToParent(uiTemplateEditor.createUI());
return uiTemplateEditor;
}

protected void addEditorUIToParent(Component editorUI) {
parentContainer.clear();
parentContainer.add(editorUI);
}

protected abstract UITemplateEditor buildEditor(EditableTemplate template);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.options.SearchableConfigurable;
import com.itangcent.idea.plugin.template.manager.EditableTemplateManager;
import com.itangcent.icode.api.EditableTemplateManager;
import com.itangcent.idea.plugin.ui.ICodeLessConfigurableGUI;

import javax.swing.*;
Expand All @@ -14,7 +14,7 @@ public class ICodeLessConfigurable implements SearchableConfigurable {
private EditableTemplateManager templateManager = ServiceManager.getService(EditableTemplateManager.class);

public boolean isModified() {
return templateManager.isModified();
return templateManager.isAnyModified();
}

public String getId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.itangcent.idea.plugin.fake;

import com.itangcent.icode.api.Template;
import com.itangcent.icode.api.EditableTemplate;
import com.itangcent.icode.api.EditableTemplateManager;
import com.itangcent.icode.api.TemplateContext;
import com.itangcent.idea.plugin.template.manager.EditableTemplateManager;

public class FakeTemplateManager implements EditableTemplateManager {
@Override
public boolean isModified() {
public boolean isAnyModified() {
return false;
}

Expand All @@ -16,12 +16,12 @@ public void save() {
}

@Override
public Template findByName(String name) {
public EditableTemplate findByName(String name) {
return null;
}

@Override
public Template findByName(String name, TemplateContext context) {
public EditableTemplate findByName(String name, TemplateContext context) {
return null;
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion icodeless-idea/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
instance="com.itangcent.idea.plugin.actions.ICodeLessConfigurable"/>

<applicationService
serviceInterface="com.itangcent.idea.plugin.template.manager.EditableTemplateManager"
serviceInterface="com.itangcent.icode.api.EditableTemplateManager"
serviceImplementation="com.itangcent.idea.plugin.fake.FakeTemplateManager">

</applicationService>
Expand Down

0 comments on commit 55048b8

Please sign in to comment.