Skip to content

Commit

Permalink
持久化
Browse files Browse the repository at this point in the history
  • Loading branch information
muedsa committed Jun 17, 2019
1 parent 05e366f commit 9a06da1
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
6 changes: 3 additions & 3 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<description><![CDATA[
A simple text reader inside intellij.<br>
<em>悄咪咪地看小说</em>
<em>XD</em>
]]></description>

<change-notes><![CDATA[
Expand All @@ -20,12 +20,12 @@

<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
<!-- uncomment to enable plugin in all products
<depends>com.intellij.modules.lang</depends>
-->


<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
<applicationService serviceImplementation="com.muedsa.intellij.textReader.state.TextReaderStateService"/>
<toolWindow id="TextReaderTool" anchor="left" factoryClass="com.muedsa.intellij.textReader.factory.ToolWindowFactoryImpl"/>
</extensions>

Expand Down
11 changes: 9 additions & 2 deletions src/com/muedsa/intellij/textReader/TextFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;

import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.nio.charset.Charset;

public class TextFile {
Expand All @@ -24,6 +23,14 @@ public TextFile(VirtualFile file) throws IOException {
charset = detector.detectCodepage(inputStream, 200);
}

public TextFile(String filePath) throws IOException {
this.filePath = filePath;
inputStream = new BufferedInputStream(new FileInputStream(new File(filePath)));
CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
detector.add(JChardetFacade.getInstance());
charset = detector.detectCodepage(inputStream, 200);
}

public String getFilePath() {
return filePath;
}
Expand Down
22 changes: 21 additions & 1 deletion src/com/muedsa/intellij/textReader/composes/ReaderWindow.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.muedsa.intellij.textReader.composes;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
Expand All @@ -11,6 +11,7 @@
import com.muedsa.intellij.textReader.TextFile;
import com.muedsa.intellij.textReader.factory.NotificationFactory;
import com.muedsa.intellij.textReader.file.TextFileChooserDescriptor;
import com.muedsa.intellij.textReader.state.TextReaderStateService;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
Expand All @@ -37,11 +38,14 @@ public class ReaderWindow {

private TextFile textFile;

private TextReaderStateService textReaderStateService;

public ReaderWindow(Project project, ToolWindow toolWindow) {
this.project = project;
this.toolWindow = toolWindow;
new NotificationFactory(project);
createUIComponents();
init();
}

private void createUIComponents(){
Expand Down Expand Up @@ -71,6 +75,8 @@ public void actionPerformed(ActionEvent e) {
textFile = new TextFile(file);
Vector<Chapter> list = Chapter.getChapters(textFile, chapterPrefix.getText(), chapterSuffix.getText());
titleList.setListData(list);
textReaderStateService.setFilePath(textFile.getFilePath());
textReaderStateService.setChapters(list);
}
catch (IOException error){
error.printStackTrace();
Expand Down Expand Up @@ -115,6 +121,20 @@ public void mouseClicked(MouseEvent e) {
});
}

private void init(){
textReaderStateService = ServiceManager.getService(TextReaderStateService.class);
if(textReaderStateService != null && textReaderStateService.getFilePath() != null && textReaderStateService.getChapters() != null){
try {
textFile = new TextFile(textReaderStateService.getFilePath());
titleList.setListData(textReaderStateService.getChapters());
}
catch (IOException error){
error.printStackTrace();
NotificationFactory.sendNotify("持久化文件加载错误", error.getLocalizedMessage(), NotificationType.ERROR);
}
}
}

public JPanel getContent(){
return readerPanel;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.muedsa.intellij.textReader.state;

import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import com.muedsa.intellij.textReader.Chapter;
import org.jetbrains.annotations.NotNull;

import java.util.Vector;
@State(name = "com.muedsa.intellij.textReader.chapter", storages = {@Storage(value = "$APP_CONFIG$/muedsa_tr.xml")})
public class TextReaderStateService implements PersistentStateComponent<TextReaderStateService> {

private Vector<Chapter> chapters = new Vector<>();

private String filePath;

public Vector<Chapter> getChapters() {
return chapters;
}

public void setChapters(Vector<Chapter> chapters) {
this.chapters = chapters;
}

public String getFilePath() {
return filePath;
}

public void setFilePath(String filePath) {
this.filePath = filePath;
}

@Override
public TextReaderStateService getState() {
return this;
}

@Override
public void loadState(@NotNull TextReaderStateService state) {
XmlSerializerUtil.copyBean(state, this);
}
}

0 comments on commit 9a06da1

Please sign in to comment.