mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-03 17:59:33 +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:
parent
8195b48b5b
commit
f9d44e8f74
8 changed files with 59 additions and 21 deletions
|
@ -1,10 +1,15 @@
|
||||||
package org.zlibrary.sampleview;
|
package org.zlibrary.sampleview;
|
||||||
|
|
||||||
import org.zlibrary.core.application.ZLApplication;
|
import org.zlibrary.core.application.*;
|
||||||
|
|
||||||
|
|
||||||
public class SampleApplication extends ZLApplication {
|
public class SampleApplication extends ZLApplication {
|
||||||
public SampleApplication() {
|
public SampleApplication() {
|
||||||
super("Sample");
|
super("Sample");
|
||||||
setView(new SampleView(this, getContext()));
|
setView(new SampleView(this, getContext()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ZLKeyBindings keyBindings() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,12 @@ package org.zlibrary.sampleview;
|
||||||
import org.zlibrary.core.view.ZLView;
|
import org.zlibrary.core.view.ZLView;
|
||||||
import org.zlibrary.core.view.ZLPaintContext;
|
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 {
|
class SampleView extends ZLView {
|
||||||
SampleView(SampleApplication application, ZLPaintContext context) {
|
SampleView(SampleApplication application, ZLPaintContext context) {
|
||||||
super(application, context);
|
super(application, context);
|
||||||
|
@ -10,13 +16,31 @@ class SampleView extends ZLView {
|
||||||
|
|
||||||
public void paint() {
|
public void paint() {
|
||||||
ZLPaintContext context = getContext();
|
ZLPaintContext context = getContext();
|
||||||
context.drawLine(0, 0, context.getWidth() - 1, context.getHeight() - 1);
|
ZLModelFactory modelFactory = new ZLModelFactory();
|
||||||
context.fillRectangle(context.getWidth() / 2, context.getHeight() / 2, context.getWidth() - 2, context.getHeight() - 2);
|
ZLTextModel model = modelFactory.createPlainModel();
|
||||||
|
ZLTextParagraph paragraph = modelFactory.createParagraph();
|
||||||
String text = "Hello, World!";
|
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);
|
final int w = context.stringWidth(text);
|
||||||
context.drawString((context.getWidth() - w) / 2, context.stringHeight(), text);
|
context.drawString((context.getWidth() - w) / 2, context.stringHeight(), text);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* String text = "42";
|
||||||
|
final int w = context.stringWidth(text);
|
||||||
|
context.drawString((context.getWidth() - w) / 2, context.stringHeight(), text);
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
public String caption() {
|
public String caption() {
|
||||||
return "SampleView";
|
return "SampleView";
|
||||||
|
|
|
@ -10,17 +10,17 @@ class ZLTextControlElement extends ZLTextElement {
|
||||||
myEntry = entry;
|
myEntry = entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Kind getKind() {
|
/* public Kind getKind() {
|
||||||
return Kind.CONTROL_ELEMENT;
|
return Kind.CONTROL_ELEMENT;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
public ZLTextControlEntry getEntry() {
|
public ZLTextControlEntry getEntry() {
|
||||||
return (ZLTextControlEntry)myEntry;
|
return (ZLTextControlEntry)myEntry;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte getTextKind() {
|
/* public byte getTextKind() {
|
||||||
return getEntry().getKind();
|
return getEntry().getKind();
|
||||||
}
|
}*/
|
||||||
|
|
||||||
public boolean isStart() {
|
public boolean isStart() {
|
||||||
return getEntry().isStart();
|
return getEntry().isStart();
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
package org.zlibrary.text.view.impl;
|
package org.zlibrary.text.view.impl;
|
||||||
|
|
||||||
class ZLTextElement {
|
public class ZLTextElement {
|
||||||
|
|
||||||
enum Kind {
|
/* enum Kind {
|
||||||
WORD_ELEMENT,
|
WORD_ELEMENT,
|
||||||
CONTROL_ELEMENT,
|
CONTROL_ELEMENT,
|
||||||
|
HSPACE_ELEMENT;
|
||||||
};
|
};
|
||||||
|
|
||||||
public Kind getKind() {
|
public Kind getKind() {
|
||||||
return Kind.WORD_ELEMENT;
|
return Kind.WORD_ELEMENT;
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import org.zlibrary.text.model.entry.ZLTextEntry;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
abstract class ZLTextParagraphCursor {
|
public abstract class ZLTextParagraphCursor {
|
||||||
|
|
||||||
private static abstract class Processor {
|
private static abstract class Processor {
|
||||||
protected ZLTextParagraph myParagraph;
|
protected ZLTextParagraph myParagraph;
|
||||||
|
@ -51,12 +51,12 @@ abstract class ZLTextParagraphCursor {
|
||||||
if (dataLength != 0) {
|
if (dataLength != 0) {
|
||||||
String data = textEntry.getData();
|
String data = textEntry.getData();
|
||||||
char ch;
|
char ch;
|
||||||
int firstNonSpace = 0;
|
int firstNonSpace = -1;
|
||||||
boolean spaceInserted = false;
|
boolean spaceInserted = false;
|
||||||
for (int charPos = 0; charPos < data.length(); charPos++) {
|
for (int charPos = 0; charPos < data.length(); charPos++) {
|
||||||
char current = data.charAt(charPos);
|
char current = data.charAt(charPos);
|
||||||
if (current == ' ') {
|
if (current == ' ') {
|
||||||
if (firstNonSpace != 0) {
|
if (firstNonSpace != -1) {
|
||||||
addWord(data.substring(firstNonSpace, charPos), myOffset + (firstNonSpace - data.length()),
|
addWord(data.substring(firstNonSpace, charPos), myOffset + (firstNonSpace - data.length()),
|
||||||
charPos - firstNonSpace);
|
charPos - firstNonSpace);
|
||||||
myElements.add(new ZLTextHSpaceElement());
|
myElements.add(new ZLTextHSpaceElement());
|
||||||
|
@ -66,11 +66,11 @@ abstract class ZLTextParagraphCursor {
|
||||||
myElements.add(new ZLTextHSpaceElement());
|
myElements.add(new ZLTextHSpaceElement());
|
||||||
spaceInserted = true;
|
spaceInserted = true;
|
||||||
}
|
}
|
||||||
} else if (firstNonSpace == 0) {
|
} else if (firstNonSpace == -1) {
|
||||||
firstNonSpace = charPos;
|
firstNonSpace = charPos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (firstNonSpace != 0) {
|
if (firstNonSpace != -1) {
|
||||||
addWord(data.substring(firstNonSpace, data.length()), myOffset + (firstNonSpace - data.length()),
|
addWord(data.substring(firstNonSpace, data.length()), myOffset + (firstNonSpace - data.length()),
|
||||||
data.length() - firstNonSpace);
|
data.length() - firstNonSpace);
|
||||||
}
|
}
|
||||||
|
@ -86,6 +86,7 @@ abstract class ZLTextParagraphCursor {
|
||||||
protected ZLTextParagraphCursor(ZLTextModel model, int index) {
|
protected ZLTextParagraphCursor(ZLTextModel model, int index) {
|
||||||
myModel = model;
|
myModel = model;
|
||||||
myIndex = Math.min(index, myModel.getParagraphsNumber() - 1);
|
myIndex = Math.min(index, myModel.getParagraphsNumber() - 1);
|
||||||
|
myElements = new ArrayList <ZLTextElement> ();
|
||||||
fill();
|
fill();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,8 +96,13 @@ abstract class ZLTextParagraphCursor {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Is it ok to create new instance of Processor here?*/
|
||||||
|
|
||||||
protected void fill() {
|
protected void fill() {
|
||||||
ZLTextParagraph paragraph = myModel.getParagraph(myIndex);
|
ZLTextParagraph paragraph = myModel.getParagraph(myIndex);
|
||||||
|
if (paragraph.getKind() == ZLTextParagraph.Kind.TEXT_PARAGRAPH) {
|
||||||
|
(new StandardProcessor(paragraph, myIndex, myElements)).fill();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void clear() {
|
protected void clear() {
|
||||||
|
|
|
@ -2,7 +2,7 @@ package org.zlibrary.text.view.impl;
|
||||||
|
|
||||||
import org.zlibrary.text.model.ZLTextModel;
|
import org.zlibrary.text.model.ZLTextModel;
|
||||||
|
|
||||||
class ZLTextPlainParagraphCursor extends ZLTextParagraphCursor {
|
public class ZLTextPlainParagraphCursor extends ZLTextParagraphCursor {
|
||||||
/*package*/ ZLTextPlainParagraphCursor(ZLTextModel model, int index) {
|
/*package*/ ZLTextPlainParagraphCursor(ZLTextModel model, int index) {
|
||||||
super(model, index);
|
super(model, index);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package org.zlibrary.text.view.impl;
|
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 String myData;
|
||||||
public short mySize;
|
public short mySize;
|
||||||
public short myLength;
|
public short myLength;
|
||||||
|
@ -15,7 +17,7 @@ package org.zlibrary.text.view.impl;
|
||||||
myParagraphOffset = paragraphOffset;
|
myParagraphOffset = paragraphOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Kind getKind() {
|
/* public Kind getKind() {
|
||||||
return Kind.WORD_ELEMENT;
|
return Kind.WORD_ELEMENT;
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,7 +151,7 @@ class ZLTextWordCursor {
|
||||||
myCharNumber = 0;
|
myCharNumber = 0;
|
||||||
if (charNumber > 0) {
|
if (charNumber > 0) {
|
||||||
ZLTextElement element = myParagraphCursor.getElement(myWordNumber);
|
ZLTextElement element = myParagraphCursor.getElement(myWordNumber);
|
||||||
if (element.getKind() == ZLTextElement.Kind.WORD_ELEMENT) {
|
if (element instanceof ZLTextWord) {
|
||||||
if (charNumber <= ((ZLTextWord)element).myLength) {
|
if (charNumber <= ((ZLTextWord)element).myLength) {
|
||||||
myCharNumber = charNumber;
|
myCharNumber = charNumber;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue