1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-03 09:49:19 +02:00

Sample application showing a word from the model

git-svn-id: https://only.mawhrin.net/repos/FBReaderJ/trunk@115 6a642e6f-84f6-412e-ac94-c4a38d5a04b0
This commit is contained in:
griffon 2007-11-12 20:59:13 +00:00
parent 8195b48b5b
commit f9d44e8f74
8 changed files with 59 additions and 21 deletions

View file

@ -1,10 +1,15 @@
package org.zlibrary.sampleview;
import org.zlibrary.core.application.ZLApplication;
import org.zlibrary.core.application.*;
public class SampleApplication extends ZLApplication {
public SampleApplication() {
super("Sample");
setView(new SampleView(this, getContext()));
}
public ZLKeyBindings keyBindings() {
return null;
}
}

View file

@ -3,6 +3,12 @@ package org.zlibrary.sampleview;
import org.zlibrary.core.view.ZLView;
import org.zlibrary.core.view.ZLPaintContext;
import org.zlibrary.text.model.*;
import org.zlibrary.text.model.impl.ZLModelFactory;
import org.zlibrary.text.model.entry.*;
import org.zlibrary.text.view.impl.*;
class SampleView extends ZLView {
SampleView(SampleApplication application, ZLPaintContext context) {
super(application, context);
@ -10,12 +16,30 @@ class SampleView extends ZLView {
public void paint() {
ZLPaintContext context = getContext();
context.drawLine(0, 0, context.getWidth() - 1, context.getHeight() - 1);
context.fillRectangle(context.getWidth() / 2, context.getHeight() / 2, context.getWidth() - 2, context.getHeight() - 2);
ZLModelFactory modelFactory = new ZLModelFactory();
ZLTextModel model = modelFactory.createPlainModel();
ZLTextParagraph paragraph = modelFactory.createParagraph();
ZLTextEntry entry;
entry = modelFactory.createTextEntry("griffon");
paragraph.addEntry(entry);
model.addParagraphInternal(paragraph);
int paragraphs = model.getParagraphsNumber();
for (int i = 0; i < paragraphs; i++) {
ZLTextParagraphCursor cursor = ZLTextParagraphCursor.getCursor(model, i);
for (int j = 0; j < cursor.getParagraphLength(); j++) {
ZLTextElement element = cursor.getElement(j);
if (element instanceof ZLTextWord) {
String text = ((ZLTextWord) element).myData;
final int w = context.stringWidth(text);
context.drawString((context.getWidth() - w) / 2, context.stringHeight(), text);
}
}
}
String text = "Hello, World!";
/* String text = "42";
final int w = context.stringWidth(text);
context.drawString((context.getWidth() - w) / 2, context.stringHeight(), text);
*/
}
public String caption() {

View file

@ -10,17 +10,17 @@ class ZLTextControlElement extends ZLTextElement {
myEntry = entry;
}
public Kind getKind() {
/* public Kind getKind() {
return Kind.CONTROL_ELEMENT;
}
}*/
public ZLTextControlEntry getEntry() {
return (ZLTextControlEntry)myEntry;
}
public byte getTextKind() {
/* public byte getTextKind() {
return getEntry().getKind();
}
}*/
public boolean isStart() {
return getEntry().isStart();

View file

@ -1,13 +1,14 @@
package org.zlibrary.text.view.impl;
class ZLTextElement {
public class ZLTextElement {
enum Kind {
/* enum Kind {
WORD_ELEMENT,
CONTROL_ELEMENT,
HSPACE_ELEMENT;
};
public Kind getKind() {
return Kind.WORD_ELEMENT;
}
}*/
}

View file

@ -7,7 +7,7 @@ import org.zlibrary.text.model.entry.ZLTextEntry;
import java.util.*;
abstract class ZLTextParagraphCursor {
public abstract class ZLTextParagraphCursor {
private static abstract class Processor {
protected ZLTextParagraph myParagraph;
@ -51,12 +51,12 @@ abstract class ZLTextParagraphCursor {
if (dataLength != 0) {
String data = textEntry.getData();
char ch;
int firstNonSpace = 0;
int firstNonSpace = -1;
boolean spaceInserted = false;
for (int charPos = 0; charPos < data.length(); charPos++) {
char current = data.charAt(charPos);
if (current == ' ') {
if (firstNonSpace != 0) {
if (firstNonSpace != -1) {
addWord(data.substring(firstNonSpace, charPos), myOffset + (firstNonSpace - data.length()),
charPos - firstNonSpace);
myElements.add(new ZLTextHSpaceElement());
@ -66,11 +66,11 @@ abstract class ZLTextParagraphCursor {
myElements.add(new ZLTextHSpaceElement());
spaceInserted = true;
}
} else if (firstNonSpace == 0) {
} else if (firstNonSpace == -1) {
firstNonSpace = charPos;
}
}
if (firstNonSpace != 0) {
if (firstNonSpace != -1) {
addWord(data.substring(firstNonSpace, data.length()), myOffset + (firstNonSpace - data.length()),
data.length() - firstNonSpace);
}
@ -86,6 +86,7 @@ abstract class ZLTextParagraphCursor {
protected ZLTextParagraphCursor(ZLTextModel model, int index) {
myModel = model;
myIndex = Math.min(index, myModel.getParagraphsNumber() - 1);
myElements = new ArrayList <ZLTextElement> ();
fill();
}
@ -95,8 +96,13 @@ abstract class ZLTextParagraphCursor {
return result;
}
/*Is it ok to create new instance of Processor here?*/
protected void fill() {
ZLTextParagraph paragraph = myModel.getParagraph(myIndex);
if (paragraph.getKind() == ZLTextParagraph.Kind.TEXT_PARAGRAPH) {
(new StandardProcessor(paragraph, myIndex, myElements)).fill();
}
}
protected void clear() {

View file

@ -2,7 +2,7 @@ package org.zlibrary.text.view.impl;
import org.zlibrary.text.model.ZLTextModel;
class ZLTextPlainParagraphCursor extends ZLTextParagraphCursor {
public class ZLTextPlainParagraphCursor extends ZLTextParagraphCursor {
/*package*/ ZLTextPlainParagraphCursor(ZLTextModel model, int index) {
super(model, index);
}

View file

@ -1,6 +1,8 @@
package org.zlibrary.text.view.impl;
/*package*/ class ZLTextWord extends ZLTextElement {
/*Should we use this special comment for package local things?*/
public class ZLTextWord extends ZLTextElement {
public String myData;
public short mySize;
public short myLength;
@ -15,7 +17,7 @@ package org.zlibrary.text.view.impl;
myParagraphOffset = paragraphOffset;
}
public Kind getKind() {
/* public Kind getKind() {
return Kind.WORD_ELEMENT;
}
}*/
}

View file

@ -151,7 +151,7 @@ class ZLTextWordCursor {
myCharNumber = 0;
if (charNumber > 0) {
ZLTextElement element = myParagraphCursor.getElement(myWordNumber);
if (element.getKind() == ZLTextElement.Kind.WORD_ELEMENT) {
if (element instanceof ZLTextWord) {
if (charNumber <= ((ZLTextWord)element).myLength) {
myCharNumber = charNumber;
}