2010. 6. 29. 15:53
매번 테이블을 만들기도 귀찮고.. 테이블을 동적으로 생성하게 만들어 놓으면 혹시나.. 서버에서 보내는 데이터를 토대로 테이블을 만들어 주면 좋겟다.. 라는 생각으로 만들게됨.
ps.
prototype 으로 제작했기 때문에 추후 need가 생기면.. 참고로 정식 버전을 작성하면 될듯.
sample xml
sample.xml 과 매핑되는 Class 생성 (JAXB 사용)
XML Bind
Table 생성
Launcher
ps.
prototype 으로 제작했기 때문에 추후 need가 생기면.. 참고로 정식 버전을 작성하면 될듯.
sample xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tableconfig>
<tables>
<id>table_body_result</id>
<fields>
<text>no</text>
<size></size>
</fields>
<fields>
<text>command</text>
<size></size>
</fields>
<fields>
<text>data</text>
<size></size>
</fields>
<fields>
<text>sw</text>
<size></size>
</fields>
<fields>
<text>response</text>
<size></size>
</fields>
</tables>
<tables>
<id>command_table</id>
<fields>
<text>no</text>
<size></size>
</fields>
<fields>
<text>command</text>
<size></size>
</fields>
<fields>
<text>data</text>
<size></size>
</fields>
</tables>
</tableconfig>
<tableconfig>
<tables>
<id>table_body_result</id>
<fields>
<text>no</text>
<size></size>
</fields>
<fields>
<text>command</text>
<size></size>
</fields>
<fields>
<text>data</text>
<size></size>
</fields>
<fields>
<text>sw</text>
<size></size>
</fields>
<fields>
<text>response</text>
<size></size>
</fields>
</tables>
<tables>
<id>command_table</id>
<fields>
<text>no</text>
<size></size>
</fields>
<fields>
<text>command</text>
<size></size>
</fields>
<fields>
<text>data</text>
<size></size>
</fields>
</tables>
</tableconfig>
sample.xml 과 매핑되는 Class 생성 (JAXB 사용)
- Fields.java
- Tableconfig.java
- Tables.java
- Tableconfig.java
- Tables.java
XML Bind
package jface.common.table;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.UnmarshallerHandler;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import jface.common.constants.Constants;
import jface.common.table.generated.Tableconfig;
public class TableXmlBinding {
public Tableconfig getTableconfig() {
try {
ClassLoader cl = this.getClass().getClassLoader();
JAXBContext context = JAXBContext.newInstance(Tableconfig.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
// Unmarshaller에서 저수준 처리기를 얻는다.
UnmarshallerHandler unmarshallerHandler = unmarshaller.getUnmarshallerHandler();
// SAX 파서를 얻기 위해 JAXP를 사용한다.
SAXParserFactory factory = SAXParserFactory.newInstance();
// factory 옵션을 설정한 다음, 표준 JAXP 호출을 사용한다.
factory.setNamespaceAware(true);
XMLReader reader = factory.newSAXParser().getXMLReader();
// unmarshallerHandler로 reader 처리기를 설정한다.
reader.setContentHandler(unmarshallerHandler);
// 이제 JAXB에서 가져온 언마샬링 처리기를 사용해 구문분석에 들어간다.
reader.parse(new InputSource(cl.getResourceAsStream(Constants.tableconfig)));
Tableconfig instance = (Tableconfig)unmarshallerHandler.getResult();
return instance;
} catch(Exception e) {
e.printStackTrace();
return null;
}
}
}
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.UnmarshallerHandler;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import jface.common.constants.Constants;
import jface.common.table.generated.Tableconfig;
public class TableXmlBinding {
public Tableconfig getTableconfig() {
try {
ClassLoader cl = this.getClass().getClassLoader();
JAXBContext context = JAXBContext.newInstance(Tableconfig.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
// Unmarshaller에서 저수준 처리기를 얻는다.
UnmarshallerHandler unmarshallerHandler = unmarshaller.getUnmarshallerHandler();
// SAX 파서를 얻기 위해 JAXP를 사용한다.
SAXParserFactory factory = SAXParserFactory.newInstance();
// factory 옵션을 설정한 다음, 표준 JAXP 호출을 사용한다.
factory.setNamespaceAware(true);
XMLReader reader = factory.newSAXParser().getXMLReader();
// unmarshallerHandler로 reader 처리기를 설정한다.
reader.setContentHandler(unmarshallerHandler);
// 이제 JAXB에서 가져온 언마샬링 처리기를 사용해 구문분석에 들어간다.
reader.parse(new InputSource(cl.getResourceAsStream(Constants.tableconfig)));
Tableconfig instance = (Tableconfig)unmarshallerHandler.getResult();
return instance;
} catch(Exception e) {
e.printStackTrace();
return null;
}
}
}
Table 생성
package jface.common.table;
import java.util.List;
import jface.common.table.generated.Fields;
import jface.common.table.generated.Tableconfig;
import jface.common.table.generated.Tables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
public class DefineTable {
Table makedTable;
public DefineTable(Composite parent, int style) {
makedTable = new Table(parent, style);
}
public void makeTable(String tableId) {
TableXmlBinding bind = new TableXmlBinding();
Tableconfig config = bind.getTableconfig();
List tables = config.getTables();
for(int i = 0;i<tables.size();i++) {
Tables table = (Tables)tables.get(i);
String id = table.getId();
if(id.equals(tableId)) {
List fields = table.getFields();
for(int j = 0;j<fields.size();j++) {
Fields field = (Fields)fields.get(j);
TableColumn col = new TableColumn (makedTable, SWT.CENTER);
col.setText(field.getText());
if(field.getSize() > 0) {
col.setWidth(field.getSize());
}
col.pack();
}
}
}
}
public Table getMakedTable() {
return makedTable;
}
public void setMakedTable(Table makedTable) {
this.makedTable = makedTable;
}
}
import java.util.List;
import jface.common.table.generated.Fields;
import jface.common.table.generated.Tableconfig;
import jface.common.table.generated.Tables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
public class DefineTable {
Table makedTable;
public DefineTable(Composite parent, int style) {
makedTable = new Table(parent, style);
}
public void makeTable(String tableId) {
TableXmlBinding bind = new TableXmlBinding();
Tableconfig config = bind.getTableconfig();
List tables = config.getTables();
for(int i = 0;i<tables.size();i++) {
Tables table = (Tables)tables.get(i);
String id = table.getId();
if(id.equals(tableId)) {
List fields = table.getFields();
for(int j = 0;j<fields.size();j++) {
Fields field = (Fields)fields.get(j);
TableColumn col = new TableColumn (makedTable, SWT.CENTER);
col.setText(field.getText());
if(field.getSize() > 0) {
col.setWidth(field.getSize());
}
col.pack();
}
}
}
}
public Table getMakedTable() {
return makedTable;
}
public void setMakedTable(Table makedTable) {
this.makedTable = makedTable;
}
}
Launcher
{
Group group = new Group(this, SWT.NONE);
group.setBackground(Display.getDefault().getSystemColor (SWT.COLOR_BLACK));
DefineTable defineTable = new DefineTable(group, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.READ_ONLY | SWT.FULL_SELECTION);
defineTable.makeTable(Constants.table_body_result);
detailTable = defineTable.getMakedTable();
detailTable.setLinesVisible (true);
detailTable.setHeaderVisible (true);
GridLayoutFactory.fillDefaults().generateLayout(group);
}
Group group = new Group(this, SWT.NONE);
group.setBackground(Display.getDefault().getSystemColor (SWT.COLOR_BLACK));
DefineTable defineTable = new DefineTable(group, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.READ_ONLY | SWT.FULL_SELECTION);
defineTable.makeTable(Constants.table_body_result);
detailTable = defineTable.getMakedTable();
detailTable.setLinesVisible (true);
detailTable.setHeaderVisible (true);
GridLayoutFactory.fillDefaults().generateLayout(group);
}
'java > swt' 카테고리의 다른 글
[swt] - TreeTable TreeEditor (0) | 2010.06.29 |
---|---|
[swt] Progress Bar(??) (0) | 2010.06.29 |