Java で XFDL レンダリング アプリケーションを開発しています。XFDL は、XML を拡張したフォーム定義言語です。
レンダリング クラスは、StAX を使用してドキュメントを処理し、必要なアイテムをカスタマイズされた JPanel に追加して表示します。JPanel 行にフィールド (JTextFields) とラベル (JLabels) を適切に追加することに成功しました。複数のページを持つフォームを作成する場合、フォームを表示するたびに、すべてのページがフォームの最後のページの行を取得します。
コード (以下を参照) を調べましたが、この動作の原因を突き止めることができません。ステップスルーすると、すべてのカウンターが正しくインクリメントされるため、適切なページに行が追加されますが、ラベルとフィールドの現在のページの後ろに常に行の最後のページが表示されます。
Java が描画を処理する方法について何か誤解していると思われますが、何が原因かはわかりません。
回答とアドバイスをお寄せいただきありがとうございます。
XFDL ドキュメントでは、行は次のように定義されています。
<page SID="PAGE1">
<line sid="LINE1">
<itemlocation>
<ae>
<ae>absolute</ae>
<ae>1219</ae>
<ae>75</ae>
</ae>
<ae>
<ae>extent</ae>
<ae>3</ae>
<ae>854</ae>
</ae>
</itemlocation>
</line>
...
</page>
<page SID="PAGE2">
<line sid="LINE2">
<itemlocation>
<ae>
<ae>absolute</ae>
<ae>1219</ae>
<ae>75</ae>
</ae>
<ae>
<ae>extent</ae>
<ae>3</ae>
<ae>854</ae>
</ae>
</itemlocation>
</line>
...
</page>
レンダリング クラスがタグに到達すると、次のメソッドが呼び出されます。
private ArrayList<FormPanel> pages;
/**
* Draws a new line on the Panel
*
* Start State: Cursor is positioned on the <line> start element.
* End State: Cursor is positioned on the <line> end element.
*
* @throws XMLStreamException
*/
private void addLine() throws XMLStreamException {
while(reader.hasNext() &&
!(reader.isEndElement() &&
reader.getLocalName().equals(XFDL_LINE))) {
reader.next();
if(reader.isStartElement()) {
if(reader.getLocalName().equals(XFDL_ITEMLOCATION)) {
pages.get(currentPage).addLine(processItemLocation());
}
}
}
}
/**
* Processes an item location field into a Rectangle object used to set
* the bounds and location of form item.
* Start State: Cursor positioned on the itemlocation start element.
* End State: Cursor positioned on the itemlocation end element
*
* Currently only handles absolute positioning and extent sizes.
* Any other form of positioning data returns null.
*
* @return Rectangle representing the location and size of item.
* @throws XMLStreamException
*/
private Rectangle processItemLocation() throws XMLStreamException {
Rectangle result = new Rectangle();
ArrayList<String> attributes = new ArrayList<String>();
while(reader.hasNext() &&
!(reader.isEndElement() &&
reader.getLocalName().equals(XFDL_ITEMLOCATION)))
{
reader.next();
if(reader.isCharacters() && !reader.isWhiteSpace()) {
attributes.add(reader.getText());
}
}
for(int x=0; x<attributes.size(); x++) {
if(attributes.get(x).equals(XFDL_LOCATION_STYLE_ABSOLUTE)) {
result.setLocation(Integer.parseInt(attributes.get(x+1)),
Integer.parseInt(attributes.get(x+2)));
} else if(attributes.get(x).equals(XFDL_LOCATION_SIZE_EXTENT)) {
result.setSize(Integer.parseInt(attributes.get(x+1)),
Integer.parseInt(attributes.get(x+2)));
} else {
try{
Integer.parseInt(attributes.get(x));
} catch (NumberFormatException nfe) {
result = null;
x = attributes.size();
}
}
}
return result;
}
FormPanel は JPanel の拡張であり、追加のメソッド addLine() とオーバーライドされた paintComponent() を以下に示します。
private static ArrayList<Rectangle> lines;
public void addLine(Rectangle line) {
lines.add(line);
}
/**
* Overrides JPanel paintComponenet and draws lines on the form.
*/
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for(int x = 0; x<lines.size(); x++) {
g.drawRect(lines.get(x).x,
lines.get(x).y,
lines.get(x).width,
lines.get(x).height);
g.fillRect(lines.get(x).x,
lines.get(x).y,
lines.get(x).width,
lines.get(x).height);
}
}