
com.viaoa.jfc.editor.html.control.BlockController 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.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
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.CSS;
import javax.swing.text.html.HTML;
import javax.swing.text.html.CSS.Attribute;
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.Block;
import com.viaoa.jfc.editor.html.oa.DocElement;
import com.viaoa.jfc.editor.html.view.BlockDialog;
/**
* Controller for BlockDialog, used to edit a paragraph (block) attributes.
* @author vvia
*
*/
public class BlockController {
private OAHTMLTextPane textPane;
private Hub hubRootDocElement;
private Hub hubDocElement; // recursive detail hub
private Hub hubBlock;
private BlockDialog dlgBlock;
private Element element;
private AttributeSet attributeSet;
public BlockController(OAHTMLTextPane textPane) {
this.textPane = textPane;
}
public Hub getBlockHub() {
if (hubBlock == null) {
hubBlock = new Hub(Block.class);
hubBlock.add(new Block());
hubBlock.setPos(0);
}
return hubBlock;
}
public Block getBlock() {
Block block = getBlockHub().getAt(0);
return block;
}
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();
updateBlock();
}
});
}
return hubDocElement;
}
public BlockDialog getBlockDialog() {
if (dlgBlock == null) {
dlgBlock = new BlockDialog(SwingUtilities.getWindowAncestor(textPane), getBlockHub(), 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 dlgBlock;
}
protected void initSelectedElement() {
getRootDocElements().clear();
hmDocElement.clear();
hmElement.clear();
OAHTMLDocument doc = (OAHTMLDocument) textPane.getDocument();
loadElements(doc.getRootElements()[0], getRootDocElements());
hubRootDocElement.setPos(0);
int 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);
getBlockDialog().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) s = s.substring(0, pos);
if (s.indexOf('?') > 0) s += "&";
else s += "?";
s += "w="+w+"&h="+h+"";
s = "url('" + s.substring(4) + "')";
style += "background-image:" + s + ";";
}
AttributeSet asx = doc.getStyleSheet().getDeclaration(style);
sas.addAttributes(asx);
((OAHTMLDocument) textPane.getDocument()).setAttributes(element, sas);
/*was
if (attributeSet.getAttribute(StyleConstants.NameAttribute) == HTML.Tag.DIV) {
((OAHTMLDocument) textPane.getDocument()).setAttributes(element, sas);
}
else {
if (attributeSet.getAttribute(StyleConstants.NameAttribute) == HTML.Tag.IMPLIED) {
sas.removeAttribute(StyleConstants.NameAttribute);
sas.addAttribute(StyleConstants.NameAttribute, HTML.Tag.P);
}
((OAHTMLDocument) textPane.getDocument()).setParagraphAttributes(position, 1, sas, true);
}
*/
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy