
com.viaoa.jfc.editor.html.control.InsertController Maven / Gradle / Ivy
/* Copyright 1999-2015 Vince Via [email protected]
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.viaoa.jfc.editor.html.control;
import java.util.*;
import javax.swing.SwingUtilities;
import javax.swing.text.AttributeSet;
import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTML;
import com.viaoa.hub.*;
import com.viaoa.util.*;
import com.viaoa.jfc.editor.html.OAHTMLDocument;
import com.viaoa.jfc.editor.html.OAHTMLEditorKit;
import com.viaoa.jfc.editor.html.OAHTMLTextPane;
import com.viaoa.jfc.editor.html.oa.DocElement;
import com.viaoa.jfc.editor.html.oa.Insert;
import com.viaoa.jfc.editor.html.view.InsertDialog;
/**
* Controller for InsertDialog, which allows inserting html & attributes.
* @author vvia
*
*/
public class InsertController {
private OAHTMLTextPane textPane;
private Hub hubRootDocElement;
private Hub hubDocElement; // recursive detail hub
private Hub hubInsert;
private InsertDialog dlgInsert;
private Element element;
private AttributeSet attributeSet;
private int position;
public InsertController(OAHTMLTextPane textPane) {
this.textPane = textPane;
}
public Hub getInsertHub() {
if (hubInsert == null) {
hubInsert = new Hub(Insert.class);
hubInsert.add(new Insert());
hubInsert.setPos(0);
}
return hubInsert;
}
public Insert getInsert() {
Insert ins = getInsertHub().getAt(0);
return ins;
}
public Hub getRootDocElements() {
if (hubRootDocElement == null) {
hubRootDocElement = new Hub(DocElement.class);
}
return hubRootDocElement;
}
public Hub getDocElements() {
if (hubDocElement == null) {
hubDocElement = new DetailHub(getRootDocElements(), "docElements");
hubDocElement.addHubListener(new HubListenerAdapter() {
@Override
public void afterChangeActiveObject(HubEvent e) {
DocElement de = (DocElement) e.getObject();
if (de == null) return;
element = hmElement.get(de);
attributeSet = element.getAttributes();
position = element.getStartOffset();
}
});
}
return hubDocElement;
}
public InsertDialog getInsertDialog() {
if (dlgInsert == null) {
dlgInsert = new InsertDialog(SwingUtilities.getWindowAncestor(textPane), getInsertHub(), getRootDocElements(), getDocElements()) {
@Override
protected void onApply() {
apply();
}
@Override
protected void onOk() {
apply();
super.onOk();
}
@Override
public void setVisible(boolean b) {
if (b) initSelectedElement();
super.setVisible(b);
}
};
}
return dlgInsert;
}
protected void initSelectedElement() {
getRootDocElements().clear();
hmDocElement.clear();
hmElement.clear();
OAHTMLDocument doc = (OAHTMLDocument) textPane.getDocument();
loadElements(doc.getRootElements()[0], getRootDocElements());
hubRootDocElement.setPos(0);
position = textPane.getCaretPosition();
element = doc.getParagraphElement(position);
attributeSet = element.getAttributes();
DocElement de = hmDocElement.get(element);
getDocElements().setAO(de);
// set tree node
ArrayList al = new ArrayList();
Element ele = element;
for ( ; ele != null; ele = ele.getParentElement()) {
de = hmDocElement.get(ele);
al.add(0, de);
}
DocElement[] des = new DocElement[al.size()];
al.toArray(des);
getInsertDialog().getTreeComboBox().getTree().setSelectedNode(des);
}
private HashMap hmElement = new HashMap();
private HashMap hmDocElement = new HashMap();
private void loadElements(Element ele, Hub hubDocElement) {
DocElement de = new DocElement();
hmElement.put(de, ele);
hmDocElement.put(ele, de);
String name = ele.getName() + "["+ele.getStartOffset()+","+ele.getEndOffset()+"] ";
hubDocElement.add(de);
int x = ele.getElementCount();
for (int i=0; i", 0, 0, HTML.Tag.BR);
doc.remove(position, 1);
doc.remove(position+1, 1);
break;
case Insert.LOCATION_Before:
doc.insertBeforeStart(element, "
");
break;
case Insert.LOCATION_After:
doc.insertAfterEnd(element, "
");
break;
}
return;
}
if (insert.getType() == Insert.TYPE_P) {
switch (insert.getLocation()) {
case Insert.LOCATION_Inside:
// see OAHTMLEditorKit.InsertBreakAction inner class
textPane.replaceSelection("\n");
SimpleAttributeSet sas = new SimpleAttributeSet(attributeSet);
Element elemx = doc.getParagraphElement(position+1);
sas.addAttribute(StyleConstants.NameAttribute, HTML.Tag.P);
doc.setParagraphAttributes(position+1, 1, sas, true);
break;
case Insert.LOCATION_Before:
doc.insertBeforeStart(element, "");
break;
case Insert.LOCATION_After:
doc.insertAfterEnd(element, "");
break;
}
return;
}
if (insert.getType() == Insert.TYPE_DIV) {
switch (insert.getLocation()) {
case Insert.LOCATION_Inside:
doc.insertString(position, " ", new SimpleAttributeSet(attributeSet)); // adds "fake" chars so that insert is not at begin/end of line
kit.insertHTML(doc, position+1, "", 0, 0, HTML.Tag.DIV);
doc.remove(position, 1);
doc.remove(position+1, 1);
break;
case Insert.LOCATION_Before:
doc.insertBeforeStart(element, "");
break;
case Insert.LOCATION_After:
doc.insertAfterEnd(element, "");
break;
}
return;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy