Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

56
LINES

< > BotCompany Repo | #1010263 // verticallyCenterTextPane [quite complicated, seems to work, but breaks horizontal center, better use jFullCenter]

JavaX fragment (include)

1  
static JTextPane verticallyCenterTextPane(final JTextPane tp) {
2  
  swing {
3  
    tp.setEditorKit(new MyEditorKit); // TODO: rename classes
4  
  }
5  
  ret tp;
6  
}
7  
8  
sclass MyEditorKit extends StyledEditorKit {
9  
    public ViewFactory getViewFactory() {
10  
        return new StyledViewFactory();
11  
    }
12  
 
13  
    static class StyledViewFactory implements ViewFactory {
14  
        public View create(Element elem) {
15  
            String kind = elem.getName();
16  
            if (kind != null) {
17  
                if (kind.equals(AbstractDocument.ContentElementName)) {
18  
19  
                    return new LabelView(elem);
20  
                } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
21  
                    return new ParagraphView(elem);
22  
                } else if (kind.equals(AbstractDocument.SectionElementName)) {
23  
24  
                    return new CenteredBoxView(elem, View.Y_AXIS);
25  
                } else if (kind.equals(StyleConstants.ComponentElementName)) {
26  
                    return new ComponentView(elem);
27  
                } else if (kind.equals(StyleConstants.IconElementName)) {
28  
29  
                    return new IconView(elem);
30  
                }
31  
            }
32  
 
33  
            return new LabelView(elem);
34  
        }
35  
    }
36  
}
37  
 
38  
sclass CenteredBoxView extends BoxView {
39  
    public CenteredBoxView(Element elem, int axis) {
40  
        super(elem,axis);
41  
    }
42  
    protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
43  
        super.layoutMajorAxis(targetSpan,axis,offsets,spans);
44  
        int textBlockHeight = 0;
45  
        int offset = 0;
46  
 
47  
        for (int i = 0; i < spans.length; i++) {
48  
49  
            textBlockHeight = spans[i];
50  
        }
51  
        offset = (targetSpan - textBlockHeight) / 2;
52  
        for (int i = 0; i < offsets.length; i++) {
53  
            offsets[i] += offset;
54  
        }
55  
    }
56  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1010263
Snippet name: verticallyCenterTextPane [quite complicated, seems to work, but breaks horizontal center, better use jFullCenter]
Eternal ID of this version: #1010263/5
Text MD5: d1628f370288f659047495564921c0b2
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-09-07 19:42:28
Source code size: 1861 bytes / 56 lines
Pitched / IR pitched: No / No
Views / Downloads: 335 / 409
Version history: 4 change(s)
Referenced in: [show references]